Skip to content

fmt.Printf("%p", &a) changes result of pointer of empty struct comparison #68701

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
lwyw opened this issue Aug 1, 2024 · 3 comments
Closed

Comments

@lwyw
Copy link

lwyw commented Aug 1, 2024

Go version

go version go1.22.5 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/weeyoung/Library/Caches/go-build'
GOENV='/Users/weeyoung/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/weeyoung/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/weeyoung/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/local/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/local/lib/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='/usr/bin/clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/weeyoung/Documents/projs/go_samples/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/gp/y_s49k7d0ks8vcy38kpltrg40000gn/T/go-build1084406235=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://go.dev/play/p/nt37zxXTC3w

What did you see happen?

Pointer comparison results are not the same after pointer print statements

	a := struct{}{}
	b := struct{}{}
	fmt.Printf("%p\n", &a)
	fmt.Printf("%p\n", &b)
	// the following produces true
	// however commenting one or all of the print statement above makes it false
	// it does not matter if the comparison is before or after the print statements of pointers
	fmt.Println(&a == &b)

What did you expect to see?

fmt.Println(&a == &b) should always produce false, the print statements should not affect its result.

@lwyw lwyw changed the title fmt.Printf("%p", &a) changes address of empty struct fmt.Printf("%p", &a) changes result of pointer of empty struct comparison Aug 1, 2024
@lwyw
Copy link
Author

lwyw commented Aug 1, 2024

closing issue since it's expected

@lwyw lwyw closed this as completed Aug 1, 2024
@ianlancetaylor
Copy link
Contributor

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

No branches or pull requests

3 participants