结构体中字段类型的改变直接造成内存对齐结果的改变,是的占用内存空间也不一样
package main import ( "fmt" "unsafe" ) func main() { var xx struct { a bool b int32 c []int } var x struct { a int32 b []int c bool } fmt.Println("SIZE") fmt.Println(unsafe.Sizeof(x)) //32 fmt.Println(unsafe.Sizeof(xx)) //40 fmt.Println(unsafe.Sizeof(x.a)) fmt.Println(unsafe.Sizeof(x.b)) fmt.Println(unsafe.Sizeof(x.c)) fmt.Println("ALIGN") fmt.Println(unsafe.Alignof(x)) fmt.Println(unsafe.Alignof(x.a)) fmt.Println(unsafe.Alignof(x.b)) fmt.Println(unsafe.Alignof(x.c)) fmt.Println("OFFSET") fmt.Println(unsafe.Offsetof(x.a)) fmt.Println(unsafe.Offsetof(x.b)) fmt.Println(unsafe.Offsetof(x.c)) }
标签:Alignof,fmt,unsafe,golang,内存,Println,对齐,Sizeof,Offsetof From: https://www.cnblogs.com/itsuibi/p/16885984.html