Why does adding fmt.Printf change the equality result of two interface variables holding empty structs in Go? [duplicate]

5 days ago 5
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,0x56d83c0

After 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:

false

Why does printing affect comparison results?

Read Entire Article