1 2 3 4 5 6 7 8
| type integer int func (i integer) String() string { return "hello" } func main() { fmt.Println(integer(5)) }
|
Println
—->Fprintln
—->p.doPrintln(a)
—->p.printArg(arg, 'v')
打断点switch f := arg.(type)
发现类型并不走switch
,而是调用p.handleMethods(verb)
,最后调用的是p.fmtString(v.String(), verb)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| switch v := p.arg.(type) { case error: handled = true defer p.catchPanic(p.arg, verb, "Error") p.fmtString(v.Error(), verb) return
case Stringer: handled = true defer p.catchPanic(p.arg, verb, "String") p.fmtString(v.String(), verb) return }
type Stringer interface { String() string } type error interface { Error() string }
|
如果自己实现了String方法,那么则会调用你实现的方法,最后输出是hello
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| type haha struct{}
func (i *haha) String() string { return "hello1" } func (i *haha) Error() string { return "hello2" } func main() { fmt.Println(&haha{}) }
[Running] go run "d:\go work\test.go" hello2
|