Skip to content

new(struct{})==new(struct{}) dissimilarity #36384

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
breakon opened this issue Jan 4, 2020 · 3 comments
Closed

new(struct{})==new(struct{}) dissimilarity #36384

breakon opened this issue Jan 4, 2020 · 3 comments

Comments

@breakon
Copy link

breakon commented Jan 4, 2020

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

$ go version 1.13 and 1.11

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dengjl/.cache/go-build"
GOENV="/home/dengjl/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dengjl/.gvm/pkgsets/go1.13/global"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/home/dengjl/.gvm/gos/go1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/dengjl/.gvm/gos/go1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build021612501=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import "fmt"

func main() {
	a := new(struct{})
	b := new(struct{})
	fmt.Println(a == b) // true

	a1 := new(struct{})
	b1 := new(struct{})
	fmt.Println(a1 == b1) // false

	a2 := new(struct{})
	b2 := new(struct{})
	fmt.Println(a2 == b2) //false
	fmt.Println(&a, &b)   //0xc00000e028 0xc00000e030

}

Let's look at the next example

	a1 := new([0]int)
	a2 := new([0]int)
	fmt.Println(a1 == a2)

What did you expect to see?

I don't understand go's = = judgment mechanism
Hope the printing result will not be affected

What did you see instead?

If I print variables, it will affect the results

If removed

fmt.Println(&a, &b)
results

false
false
false
@zigo101
Copy link

zigo101 commented Jan 4, 2020

The comparison results depend whether or not the operands are allocated on heap/stack?
https://go101.org/article/details.html#addresses-of-zeor-sized-values

This doesn't violate Go spec.

The fmt.Println call will make the values referenced by its argument allocated on heap.

@randall77
Copy link
Contributor

@go101 is right, this doesn't violate the spec. From the Go spec:

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

It can be confusing, but this is working as intended.
In general, it is best just to avoid using == on such pointers.

@breakon
Copy link
Author

breakon commented Jan 5, 2020

比较结果取决于操作数是否在堆/堆栈上分配?
https://go101.org/article/details.html#addresses-of-zeor-sized-values

这不违反Go规范。

fmt.Println调用将使由其参数引用的值分配在堆上。

Wow, this project(go101) solved a lot of my doubts.
O (∩∩) O thank you

@golang golang locked and limited conversation to collaborators Jan 4, 2021
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