Go lang empty struct confusing feature [duplicate]

半腔热情 提交于 2020-06-17 15:48:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!