Skip to content
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

go1.22.1 println & fmt.Println print different result #66287

Closed
loveinsky100 opened this issue Mar 13, 2024 · 1 comment
Closed

go1.22.1 println & fmt.Println print different result #66287

loveinsky100 opened this issue Mar 13, 2024 · 1 comment

Comments

@loveinsky100
Copy link

Go version

go1.22.1

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN='/Users/leo/go/bin'
GOCACHE='/Users/leo/Library/Caches/go-build'
GOENV='/Users/leo/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/leo/go/pkg/mod'
GONOPROXY='*.byted.org,*.everphoto.cn,git.smartisan.com'
GONOSUMDB='*.byted.org,*.everphoto.cn,git.smartisan.com'
GOOS='darwin'
GOPATH='/Users/leo/go'
GOPRIVATE='*.byted.org,*.everphoto.cn,git.smartisan.com'

What did you do?

func main() {
	l := []int{1, 2, 3}
	for i, v := range l {
		println(&i, &v)
	}
}

result is:
0xc00004c718 0xc00004c710
0xc00004c718 0xc00004c710
0xc00004c718 0xc00004c710
func main() {
	l := []int{1, 2, 3}
	for i, v := range l {
		fmt.Println(&i, &v)
	}
}

result is:
0xc000096010 0xc000096008
0xc000096030 0xc000096018
0xc000096040 0xc000096038
func main() {
	l := []int{1, 2, 3}
	for i, v := range l {
		println(&i, &v)
		fmt.Println(&i, &v)
	}
}

result is:
0xc0000120a0 0xc000012098
0xc0000120a0 0xc000012098
0xc0000120c0 0xc0000120a8
0xc0000120c0 0xc0000120a8
0xc0000120d0 0xc0000120c8
0xc0000120d0 0xc0000120c8

What did you see happen?

result is different between with println(&i, &v) & fmt.Println(&i, &v)

What did you expect to see?

0xc000096010 0xc000096008
0xc000096030 0xc000096018
0xc000096040 0xc000096038

@fzipp
Copy link
Contributor

fzipp commented Mar 13, 2024

fmt.Println causes arguments to escape to the heap, see #19720 which is a duplicate of #8618

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