type One struct { id1 int8 id2 int32 id3 int8 } type Two struct { id1 int32 id2 int8 id3 int8 } func main() { fmt.Println(unsafe.Sizeof(One{})) //size为12 fmt.Println(unsafe.Sizeof(Two{})) //size为8 fmt.Println(unsafe.Sizeof("222222222")) //size为16 fmt.Println(unsafe.Sizeof(111111111)) //size为8 fmt.Println(unsafe.Sizeof(1.1)) //size为8 }
这样我们在写代码定义结构体变量时,注意设定他的大小和顺序
对齐内容可看一篇文章:https://zhuanlan.zhihu.com/p/53413177