【golang】unsafe.Sizeof浅析
博主也是正在学习golang,在学习过程中遇到了SizeOf的问题。我原先以为,golang中的sizeof和c的sizeof差不多,但是当我开始使用的时候,才发现了许多奇怪的问题
slice := []int{
1,2,3}
fmt.Println(unsafe.Sizeof(slice)) //24
上面声明了一个切片,然后打印出sizeof的值为24,但是不管slice里的元素为多少,sizeof返回的数据都是24。
然后在官方文档上找到一句话
Sizeof takes an expression x of any type and returns the size in bytes of a hypothetical variable v as if v was declared via var v = x. The size does not include any memory possibly referenced by x. For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size of the memory referenced by the slice.