I am very new to Go, so I am sorry for that noob question.
In JavaScript console.log(window)
returns all objects inside of window
. In PHP var_dump(new DateTime())
returns all objects inside of DateTime()
.
Is there a function in Go that will return all objects from a given object? For example Println
should be returned if fmt
is given.
Try executing go doc fmt
in a terminal to produce a description of the "fmt" package and a listing of the functions it exports, or referring to the fmt
package documentation at https://golang.org. In go, as in most other static/compiled languages, users are expected to refer to documentation (or docs) which describe the programming language and its libraries. Contrast this to some dynamic/scripting languages, which often make it easy to query any object to discover its usable properties.
At runtime, you can get a helpful printout of any arbitrary value by using the %#v
formatting verb to produce a go syntax representation of the value, e.g.
xs := []int{1, 2, 3}
fmt.Printf("OK: xs=%#v
", xs)
// OK: xs=[]int{1, 2, 3}
Note that the package "fmt" is not a value in the go language so it cannot be printed at runtime as such.
Go does not have objects, but we do have custom types and interfaces that we can attach attributes, functions and other types to.
What specifically are you trying to do? If you're looking for a var_dump
:
fmt.Println
someErr := fmt.Errorf("custom type error")
fmt.Println(someErr)
Println
formats using the default formats for its operands and writes to standard output.
fmt.Printf
someErr := fmt.Errorf("custom type error")
fmt.Printf("This is an error:%v A num: %v A str", someErr, 19, "Stackoverflow")
Printf
formats according to a format specifier and writes to standard output.
fmt.Sprintf
someErr := fmt.Errorf("custom type error")
// someStr now contains the string formatted as shown below
someStr := fmt.Sprintf("This is an error:%v A num: %v A str", someErr, 19, "Stackoverflow")
Sprintf
formats according to a format specifier and returns the resulting string.
Here is an example of my personal preference when outputting var data: https://play.golang.org/p/8dpeE-fray_J
I hope this helps!
In Go is possible does something similar, but don't work for all.
func main() {
//arrays
a := []int{1,2,3,4}
fmt.Printf("%v
", a) //print [1 2 3 4]
//maps
b := map[string]int{
"a":1,
"b":2,
}
fmt.Printf("%v
", b) //print map[a:1 b:2]
//structs
c := struct{
A int
B int
}{1,2}
d := struct{
C struct{
A int
B int
}
D int
}{c,2}
fmt.Printf("%v
", d) //print {{1 2} 2}
}
See in: https://play.golang.org/p/vzlCsOG497h
If you pass fmt occurs error because it is a package. The error is:
Error: use of package fmt without selector
I hope this helps (too)!