Pointer variables in Go are stored in intptr variables, integers large enough to hold a memory address. The atomic package makes it possible to execute the same operations for other integers types. There is a package that allows unsafe pointer operations, which offers the unsafe.Pointer type that is used in atomic operations.
In the following example, we define two integer variables and their relative integer pointers. Then we execute a swap of the first pointer with the second:
v1, v2 := 10, 100
p1, p2 := &v1, &v2
log.Printf("P1: %v, P2: %v", *p1, *p2)
atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&p1)), unsafe.Pointer(p2))
log.Printf("P1: %v, P2: %v", *p1, *p2)
v1 = -10
log.Printf("P1: %v, P2: %v", *p1, *p2)
v2 = 3
log.Printf("P1: %v, P2: %v", *p1, *p2)
After the swap, both pointers are now referring to the second variable; any change to the first value does not influence the pointers. Changing the second variable changes the value referred to by the pointers.