在下面的代码中,我希望fmt.Printf("%v\n", a)调用其 myTypeB 类型成员的 String(),但这并没有发生?为什么 ?package mainimport "fmt"type myTypeA struct { b myTypeB}type myTypeB struct { c string d int}func (b myTypeB) String() string { return "myTypeB custom"}func main() { a:= myTypeA{myTypeB{"hello", 1}}; b:= myTypeB{"hello", 1} fmt.Printf("%v\n", a) fmt.Printf("%v\n", b)}
1 回答
大话西游666
TA贡献1817条经验 获得超14个赞
fmt不fmt.Stringer递归查找。
如果参数是 a fmt.Stringer,它将调用该String()方法并打印结果。如果它没有String()方法,fmt将使用反射遍历字段以获取值。
- 1 回答
- 0 关注
- 254 浏览
添加回答
举报
0/150
提交
取消
