Is there a function in Go to print all the current member names and values of an object?
I'm looking for something like PH开发者_JAVA技巧P's print_r or python's dict. Anybody know if this function exists, or its something that needs to be implemented?
There is a reflect
package in go.
You can find solution to your problem in the following article.
For printing native go objects, like maps, slices, and arrays, you can try:
fmt.Printf("%v", object)
However there isn't a general method to do it with user-defined struct types..
Try
fmt.Printf("%+v", object)
That might give you something resembling what you want.
You can try using the package dump
, which acts similar to PHP's print_r
or var_dump
.
The sources are here and the main project page is here.
Then just call dump.Dump(yourObject)
or dump.Fdump(file, yourObject)
精彩评论