Unsafe. Pointer and unintptr, both of which appear mostly in source code, are rarely used in business. But interviewers do like to ask questions during interviews.

# unsafe.pointer

The name makes it safer for Pointers. The main function of unsafer. Pointer is to convert Pointers between different types.

There is no direct conversion between types

``````func main() {
var a *int8
var b *int16
a = new(int8)
b = new(int16)
*b = 10
*a = *b
fmt.Println(a)
}
Copy the code``````
1. Initialize a pointer type int8a
2. Initialize a pointer type int16b
3. Assigns 10 to the memory pointed to by pointer B
4. Pointer A memory assigns the memory value of pointer B

Unsafe. pointer cannot use *b (type int16) as type int8 in Assignment go

Unbroadening. Pointer is used for fancy conversions

``````var a *int8
var b *int16
a = new(int8)
b = new(int16)
*b = 10
upb := unsafe.Pointer(b)
b_int8ptr := (*int8)(upb)
*a = *(b_int8ptr)
fmt.Println(*a)
Copy the code``````
1. Initialize a pointer type int8a
2. Initialize a pointer type int16b
3. Assigns 10 to the memory pointed to by pointer B
4. Retrieve the pointer to B via unsafe.pointer`upb`
5. Convert upB to int8 pointer`b_int8ptr`
6. Get the memory value of b_int8ptr assigned to the space pointed to by the address of A

10 Safer. Pointer can be converted to different types of Pointers.

# unintptr

Uintptr is really just a uint that represents addresses.

``````var a, b uintptr
a = 10
b = 10
fmt.Println(a + b)
Copy the code``````

``````var a, b *int
a, b = new(int), new(int)
c:=a+b  //invalid operation: a + b (operator + not defined on pointer)
Copy the code``````

``````var a, b *int
a, b = new(int), new(int)
c := unsafe.Pointer(a) + unsafe.Pointer(b) // invalid operation: unsafe.Pointer(a) + unsafe.Pointer(b) (operator + not defined on unsafe.Pointer)
Copy the code``````

Combined with uintptr to do the operation between addresses

``````var a, b *int
a, b = new(int), new(int)
c := uintptr(unsafe.Pointer(a)) + uintptr(unsafe.Pointer(b))
fmt.Println(c)
Copy the code``````

# Unsafe and uintptr some combination of operations

``````type User struct {
Name string
Age  int8
}
func main() {
u := &User{}
ageOffset := unsafe.Offsetof(u.Age)
*((*int)(agePtr)) = 10
fmt.Println(u.Age)
}
Copy the code``````
2. Get the Offsetof u.age via unbroadening.Offsetof
3. Uintptr transform uAddress plus age offset to find age address agePtr
4. AgePtr is converted to a pointer int, and then the memory space is assigned to 10

Combining the unsafe and uintptr, you can also assign values to certain variables, making the whole process a bit more complicated.

# conclusion

1. A Pointer of any type can be converted to Pointer
2. Pointer can be converted to a Pointer of any type
3. Uintptr can be converted to Pointer
4. Pointer can be converted to uintptr
5. Unsafe. Pointer is a common pointer type, which is used to convert Pointers of different types. Pointer operations cannot be performed and values cannot be read in memory.
6. Uintptr is used for pointer operation, GC does not use uintptr as a pointer, uintpTR cannot hold objects, targets of uintptr type will be reclaimed.