问题
Here is my code:
package main
import (
"fmt"
)
type A struct{}
func main() {
var ad, bd A
res1 := &ad == &bd
res2 := ad == bd
fmt.Println(res1, res2)// true true
fmt.Printf("%p, %p\n", &ad, &bd) // 0x57bb60, 0x57bb60
}
Now if I comment the last line of main function like this:
package main
import (
"fmt"
)
type A struct{}
func main() {
var ad, bd A
res1 := &ad == &bd
res2 := ad == bd
fmt.Println(res1, res2) // false true
//fmt.Printf("%p, %p\n", &ad, &bd)
}
The program prints false true!
How can the last line influence the output? Is there any special feature with the empty struct? Or this problem is caused by the compiler?
来源:https://stackoverflow.com/questions/62155916/go-lang-empty-struct-confusing-feature