通常,我可以打印对象的所有属性:
c.Infof("car: %+v", car)
Run Code Online (Sandbox Code Playgroud)
但是一个结构有一个String()方法.我认为这导致上面的行只打印String()方法返回的内容.
如何覆盖此并强制打印该结构的所有属性?
一个简单的解决方法是使用%#v动词:
package main
import (
"fmt"
)
type someStruct struct {
a int
b int
}
func (someStruct) String() string {
return "this is the end"
}
func main() {
fmt.Printf("%+v\n", someStruct{1, 2})
fmt.Printf("%#v\n", someStruct{1, 2})
}
Run Code Online (Sandbox Code Playgroud)
这打印:
this is the end
main.someStruct{a:1, b:2}
Run Code Online (Sandbox Code Playgroud)