Sizeof函式
func main() {
fmt.Println(unsafe.Sizeof(true))
fmt.Println(unsafe.Sizeof(int8(0)))
fmt.Println(unsafe.Sizeof(int16(10)))
fmt.Println(unsafe.Sizeof(int32(10000000)))
fmt.Println(unsafe.Sizeof(int64(10000000000000)))
fmt.Println(unsafe.Sizeof(int(10000000000000000)))
1
1
2
4
8
8
}
Offsetof 函式
func main() {
type user1 struct {
b byte
i int32
j int64
}
var u1 user1
fmt.Println("u1.b 的大小是" + helper.InterfaceHelperObject.ToString(unsafe.Sizeof(u1.b)) + "位元組")
fmt.Println("u1.b 的偏移量是 " + helper.InterfaceHelperObject.ToString(unsafe.Offsetof(u1.b)))
fmt.Println("u1.i 的大小是" + helper.InterfaceHelperObject.ToString(unsafe.Sizeof(u1.i)) + "位元組")
fmt.Println("u1.i 的偏移量是 "+ helper.InterfaceHelperObject.ToString(unsafe.Offsetof(u1.i)))
fmt.Println("u1.j 的大小是" + helper.InterfaceHelperObject.ToString(unsafe.Sizeof(u1.j)) + "位元組")
fmt.Println("u1.j 的偏移量是 "+ helper.InterfaceHelperObject.ToString(unsafe.Offsetof(u1.j)))
}
u1.b 的大小是1位元組
u1.b 的偏移量是 0
u1.i 的大小是4位元組
u1.i 的偏移量是 4
u1.j 的大小是8位元組
u1.j 的偏移量是 8
Alignof 函式
本作品採用《CC 協議》,轉載必須註明作者和本文連結
good good study day day up