This is a creation in Article, where the information may have evolved or changed.
Package unsafe//Arbitrarytype are here for the purposes of documentation only and are not actually//part of the unsafe PAC Kage. 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 is four special operations//available for type Pointer that is not available for other TYPES.//1) A Pointer value of any type can is converted to a POINTER.//2) a Pointer can is converted to a Pointer value of any TYPE.//3) a uintptr C An is converted to a POINTER.//4) a Pointer can is converted to a uintptr.//Pointer therefore allows a program to defeat The type system and read and write//arbitrary memory. It should is used with extreme Care.<span style= "color: #ff0000;" >type Pointer *arbitrarytype</span>
Unsafe.pointer points to an address of type int. The hexadecimal address is stored.
<pre name= "code" class= "CSharp" >//Sizeof returns the size in bytes occupied by the value v. The size is, that of the//, the "top level" of the value, only. For instance, if v are 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 return type in memory size </span>
Offsetof returns the within, the struct of the field represented by v,//which must is of the form structvalue.fi Eld. 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 returns the offset of the member variable </span>//Alignof returns the alignment of the value v. It is the maximum value m such//so the address of a variable with the type of V'll always being 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 alignment values with respect to the CPU </span>
Test code:
<pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" >package mainimport ("FMT" "unsafe") type person struct {Name string//Name UInt32//Age}func main () {p: = &person{name: "Chenghan", age:22}size: = unsafe. Sizeof (*p)//fmt. Println (unsafe. Sizeof (p.name)) fmt. PRINTLN ("Size of struct:", size) by: = (*[1024]byte) (unsafe.) Pointer (p)) fmt. Printf ("Unsafe Access:%p\n", unsafe.) Pointer (p)) fmt. Printf ("struct Access:%p\n", p) P1: = (*person) (unsafe. Pointer (P)) P2: = (*person) (unsafe. Pointer (by)) FMT. Println (p1.age) fmt. PRINTLN (p2.age)//offset value 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 offset:", unsafe.) Offsetof (p.name)) fmt. PRINTLN ("Age offset:", 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)))}
Operation Result:
<pre name= "code" class= "CSharp" > Structure size: 24unsafe Address: 0XC0820045C0 structure access: 0xc0820045c02222namesize:16agesize: 4namealignof:8agealignof:4name offset: 0age offset: 16name:chage:22