Table of Contents

Go - Pointer

About

Data Type - Pointer (Locator) in Go

In Go, Pointers are explicitly visible.

but there is no pointer arithmetic.

Properties

Operator

The address operator (&)

If a variable is declared var x int

var x int := 1;

The below expression &x (pronounced address of x) yields a pointer to the integer variable, that is, a value of type *int, which is pronounced pointer to int

p := &x // p, of type *int, points to x

We say:

Expressions that denote variables are the only expressions to which the address-of operator & may be applied.

Let op ! Each call of f returns a distinct value. See Go - Alias

func f() *int { 
    v := 1
    return &v 
}

fmt.Println( f() = = f()) // "false"

The variable operator (*p)

The variable to which p points is written *p. The expression *p yields the value of that variable, an int.

fmt.Println(*p) // "1" 

Since *p denotes a variable, it may also appear on the left-hand side of an assignment, in which case the assignment updates the variable.

*p = 2 // equivalent to x = 2 
fmt.Println(x) // "2"

Documentation / Reference