Pointer to integer


We can use the & prefix during the assignment that means: take the pointer to this variable and copy the pointer. This means that the new variable (b in our case) will point to the same place in memory where the value of the original variable is. The content was not copied.

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.


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