這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
package unsafe// ArbitraryType is here for the purposes of documentation only and is not actually// part of the unsafe package. It represents the type of an arbitrary Go expression.<span style="color:#ff0000;">type ArbitraryType int</span>// Pointer represents a pointer to an arbitrary type. There are four special operations// available for type Pointer that are not available for other types.//1) A pointer value of any type can be converted to a Pointer.//2) A Pointer can be converted to a pointer value of any type.//3) A uintptr can be converted to a Pointer.//4) A Pointer can be converted to a uintptr.// Pointer therefore allows a program to defeat the type system and read and write// arbitrary memory. It should be used with extreme care.<span style="color:#ff0000;">type Pointer *ArbitraryType</span>
unsafe.pointer 指向一個Int類型的地址。存放的是十六進位的地址
<pre name="code" class="csharp">// Sizeof returns the size in bytes occupied by the value v. The size is that of the// "top level" of the value only. For instance, if v is a slice, it returns the size of// the slice descriptor, not the size of the memory referenced by the slice.<span style="color:#ff0000;">func Sizeof(v ArbitraryType) uintptr 傳回型別在記憶體所佔的大小</span>
// Offsetof returns the offset within the struct of the field represented by v,// which must be of the form structValue.field. In other words, it returns the// number of bytes between the start of the struct and the start of the field.<span style="color:#ff0000;">func Offsetof(v ArbitraryType) uintptr 返回成員變數的位移量</span>// Alignof returns the alignment of the value v. It is the maximum value m such// that the address of a variable with the type of v will always be zero mod m.// If v is of the form structValue.field, it returns the alignment of field f within struct object obj.<span style="color:#ff0000;">func Alignof(v ArbitraryType) uintptr 相對於CPU的對齊值</span>
測試代碼:
<pre name="code" class="csharp"><pre name="code" class="csharp">package mainimport ("fmt""unsafe")type Person struct {name string //姓名age uint32 //年齡}func main() {p := &Person{name: "chenghan", age: 22}size := unsafe.Sizeof(*p)//fmt.Println(unsafe.Sizeof(p.name))fmt.Println("結構體的大小:", size)by := (*[1024]byte)(unsafe.Pointer(p))fmt.Printf("unsafe取址:%p\n", unsafe.Pointer(p))fmt.Printf("結構體取址:%p\n", p)p1 := (*Person)(unsafe.Pointer(p))p2 := (*Person)(unsafe.Pointer(by))fmt.Println(p1.age)fmt.Println(p2.age)//位移量取值fmt.Println("nameSize:", unsafe.Sizeof(p.name))fmt.Println("ageSize:", unsafe.Sizeof(p.age))fmt.Println("nameAlignof:", unsafe.Alignof(p.name))fmt.Println("ageAlignof:", unsafe.Alignof(p.age))fmt.Println("name位移量:", unsafe.Offsetof(p.name))fmt.Println("age位移量:", unsafe.Offsetof(p.age))fmt.Println("name:", *(*string)(unsafe.Pointer(p)))fmt.Println("age:", *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Offsetof(p.age))))}
運行結果:
<pre name="code" class="csharp">結構體的大小: 24unsafe取址:0xc0820045c0結構體取址:0xc0820045c02222nameSize: 16ageSize: 4nameAlignof: 8ageAlignof: 4name位移量: 0age位移量: 16name: chage: 22