[Go] Sizeof memory alignment and

unsafe, as the name suggests, it is unsafe, but it also has its advantages, it is possible to bypass the security mechanisms of memory Go directly to the memory read and write, sometimes because performance is required, it will take some risks with this package, memory to operate.

Sizeof amount in the end is what size?

Sizeof函数可以返回一个类型所占用的内存大小
这个大小只与类型有关,和类型对应的变量存储的内容大小无关,比如bool型占用一个字节、int8也占用一个字节。
func main() {
    fmt.Println(unsafe.Sizeof(true))                  //1
    fmt.Println(unsafe.Sizeof(int8(0)))               //1
    fmt.Println(unsafe.Sizeof(int16(10)))             //2
    fmt.Println(unsafe.Sizeof(int32(10000000)))       //4
    fmt.Println(unsafe.Sizeof(int64(10000000000000))) //8
    fmt.Println(unsafe.Sizeof(int(10000000000000000)))//8
}

Guess you like

Origin www.cnblogs.com/iiiiiher/p/12165898.html