Skip to content

Pointers to empty structs behave inconsistently #47950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lmb opened this issue Aug 25, 2021 · 1 comment
Closed

Pointers to empty structs behave inconsistently #47950

lmb opened this issue Aug 25, 2021 · 1 comment

Comments

@lmb
Copy link
Contributor

lmb commented Aug 25, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

Reproduced up to 1.14.12.

What did you do?

https://play.golang.org/p/XWirTpyJaDH

	var (
		a = &struct{}{}
		b = &struct{}{}
	)

	fmt.Printf("%p %p %t\n", a, b, a == b)

Prints something like 0x555f30 0x555f30 true. Replacing the Printf with

	fmt.Printf("%p %t\n", a, a == b)

prints 0x555f30 false.

Changing to a non-empty struct like struct { a int } makes the issue go away.

What did you expect to see?

  • a != b
  • The presence of the Printf should not influence the outcome of a == b
@D1CED
Copy link

D1CED commented Aug 25, 2021

https://golang.org/ref/spec#Comparison_operators

Pointers to distinct zero-size variables may or may not be equal.

The behavior you are observing while strange is fully compliant with the language specification.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants