- &
- *
Pointer to integer
If at this point you print out the content of the new variable, you'll see the hexadecimal value of the pointer. In order to print out the real value you need to prefix the variable by a *.
If you now change the original variable, you'll see both are incremented. If you change the new variable (you'll have to use the * prefix for this) both are incremented.
Maybe it is better to say that there is only one value jut two ways to access it.
I think you would rarely do this in such code, but it is important to understand the concept for when we will want to pass a variable by reference to a function.
- & get pointer to
- * get value behind pointer
examples/int-pointer/int_pointer.go
package main import ( "fmt" ) func main() { a := 1 b := &a fmt.Println(b) fmt.Println(*b) fmt.Printf("%v %v %T %T\n", a, *b, a, b) a++ fmt.Printf("%v %v %T %T\n", a, *b, a, b) *b = 3 fmt.Printf("%v %v %T %T\n", a, *b, a, b) }
0xc0000140e0 1 1 1 int *int 2 2 int *int 3 3 int *int