type Tt struct {
fmt.Stringer
I int
J string
k int
}
func (t *Tt) String() string {
return fmt.Sprintf("Tt{I:%d, J:%s, k:%d}", t.I, t.J, t.k)
}
func main() {
t := Tt{I: 100, J: "abc"}
fmt.Printf("%t", true)
var i any = t
v, ok := i.(fmt.Stringer)
if ok {
fmt.Printf("t --> %s", v)
} else {
fmt.Printf("t --> null")
}
}
If the address of t is assigned to v, the true branch can be executed normally. Does the interface assertion of Go language distinguish whether the receiver is of pointer type? But the interface definition ignores the receiver, which is a bit strange. ////go version go1.22.2 windows/amd64
New contributor
tanpopo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.