ARTICLE AD BOX
I'm observing a strange behavior in Go where the presence of a fmt.Printf statement affects the boolean result of an interface comparison.
Env:
go version go1.25.6 darwin/amd64 package main import "fmt" type Animal interface { Name() string } type Cat struct { } func (c Cat) Name() string { return "cat" } func main() { var c1 Animal = new(Cat) var c2 Animal = new(Cat) fmt.Println(c1 == c2) fmt.Printf("%p,%p", c1, c2) }output:
true 0x56d83c0,0x56d83c0After Delete the second to last line
package main import "fmt" type Animal interface { Name() string } type Cat struct { } func (c Cat) Name() string { return "cat" } func main() { var c1 Animal = new(Cat) var c2 Animal = new(Cat) fmt.Println(c1 == c2) // fmt.Printf("%p,%p", c1, c2) }output:
falseWhy does printing affect comparison results?
