package main
import (
"fmt"
)
func main() {
a := new(struct{})
b := new(struct{})
println(a, b, a==b)
c := new(struct{})
d := new(struct{})
fmt.Println(c, d, c==d)
}
output:
0xc000107f37 0xc000107f37 false
&{} &{} true
And, I will get different results after disabling compiler optimization.
0xc000087e4e 0xc000087e4e true
&{} &{} true
Why does the Go compiler do this optimization?
See [this line in the runtime source code ]
if size == 0 {
return unsafe.Pointer(&zerobase)
}
I think that since all zero-size variables will be allocated with zerobase, it should print true instead of false.