空结构体

来源:4-1 什么变量的大小是 0 字节?

JD

2023-09-11

有一个地方说

空结构体的地址均相同(不包含在其它结构体中时)

这里的不包含在“其它结构体”,前提是这个“其它结构体”有别的字段吧。
例如以下的例子:

type A struct {
	}

	type B struct {
	}

	type C struct {
		a A
	}

	type D struct {
		a   A
		num uint32
	}

	a := A{}
	b := B{}
	c := C{}
	d := D{}

	fmt.Printf("a: %p\n", &a)
	fmt.Printf("b: %p\n", &b)
	fmt.Printf("c.a: %p\n", &c.a)
	fmt.Printf("d.a: %p\n", &d.a)

输出的结果是:

a: 0x104c09310
b: 0x104c09310
c.a: 0x104c09310
d.a: 0x140001991a8
写回答

1回答

Moody

2023-09-15

嗯是这样的
0
0

深入Go底层原理,重写Redis中间件实战

深入Go语言原理+Go重写Redis

474 学习 · 172 问题

查看课程