golang in, unsafe.sizeof in the end is doing it?

https://www.golangtc.com/t/5ad833404ce40d2654053485

 

Niche beginner Go, a little do not know, today in order to know in the end how much of the empty structure of space time, go to Baidu said in unsafe.sizeof, but for unsafe.sizeof like sizeof and other languages ​​a little different?

such as:

var str string = "hello"
var str2 string fmt.Println(unsafe.SizeOf(str), unsafe.SizeOf(str2)) 

Two structures are printed out 16, which is why?
In the end how to get the structure of space accounted for?
Empty structures occupy much space?

Total 1 reply


heimeil  2018-04-19 19:56

Only returns the size of the data type, regardless of the size of the reference data, stringthe type of data is not directly stored, but a structure with a pointer to the actual data address

type StringHeader struct {
        Data uintptr
        Len  int
}

In the system 64 uintptr intis 8 bytes, the 16 add up.

https://golang.org/pkg/reflect/#StringHeader

Guess you like

Origin www.cnblogs.com/saolv/p/11790130.html