# 6. 大小为 0 字节的变量
空结构体的大小是 0,所有空结构体的值都指向同一个指针 zerobase
// base address for all 0-byte allocations var zerobase uintptr
示例代码:
type A struct {
}
type B struct {
a A
b int
}
func main() {
a := A{}
b := B{}
c := A{}
fmt.Printf("a pointer is: %p, size is %dbytes\n", &a, unsafe.Sizeof(a))
fmt.Printf("b pointer is: %p, size is %dbytes\n", &b, unsafe.Sizeof(b))
fmt.Printf("b.a pointer is: %p, size is %dbytes\n", &b.a, unsafe.Sizeof(b.a)) //b中的a非独立,需要依赖于b
fmt.Printf("c pointer is: %p, size is %dbytes\n", &c, unsafe.Sizeof(c))
}
//输出
/*
a pointer is: 0x1164fc0, size is 0bytes
b pointer is: 0xc000096008, size is 8bytes
b.a pointer is: 0xc00010c008, size is 0bytes
c pointer is: 0x1164fc0, size is 0bytes
*/
作用:节约内存
- 传递信号,如 channel
- HashSet 的 Value