We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
go version
$ go version 1.13 and 1.11
yes
go env
$ 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"
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)
I don't understand go's = = judgment mechanism Hope the printing result will not be affected
If I print variables, it will affect the results
If removed
fmt.Println(&a, &b) results
false false false
The text was updated successfully, but these errors were encountered:
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.
fmt.Println
Sorry, something went wrong.
@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.
比较结果取决于操作数是否在堆/堆栈上分配? https://go101.org/article/details.html#addresses-of-zeor-sized-values 这不违反Go规范。 该fmt.Println调用将使由其参数引用的值分配在堆上。
比较结果取决于操作数是否在堆/堆栈上分配? 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
No branches or pull requests
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Let's look at the next example
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
The text was updated successfully, but these errors were encountered: