Golang: Pointer

By Xah Lee. Date: . Last updated: .

Go has pointers. A pointer holds the memory address of a variable.

The purpose of pointer is mostly for speeding up computation.

For example, when you call a function

var x=3; f(x)

golang will make a copy of x, and pass it to f. If you change x in f, it does not change the variable x defined outside of it.

package main

import "fmt"

func main() {

    var x = 3

    var f = func(x int) int {
        x = 4
        return x
    }

    fmt.Printf("%v\n", f(x)) // 4
    fmt.Printf("%v\n", x)    // 3

}

However, making a copy would slow things down.

So, you have “pointers”. Think of it as address of the variable.

When you pass the address of a variable to a function, the function doesn't make a copy.

However, now if you modify x, the variable outside of f will also be modified.

package main

import "fmt"

func main() {

    var x = 3

    var f = func(x *int) int {
        *x = 4
        return *x
    }

    fmt.Printf("%v\n", f(&x)) // 4
    fmt.Printf("%v\n", x)     // 4

}

Syntax for Pointer Type

If a type name is type, then the syntax for pointer type of that type is just *type

For example:

package main

import "fmt"

func main() {

    // declare a var, named t, of type int
    var t int

    // assign value to t
    t = 3

    // declare a var, named tp, of pointer type of t
    var tp *int

    // get a pointer to t. Or, think of it as address of t
    tp = &t

    // print their type
    fmt.Printf("%T\n", t)  // int
    fmt.Printf("%T\n", tp) // *int
}

Get Pointer of a Variable

&var_name → return a pointer of variable named var_name

The & is called address operator.

package main

import "fmt"

func main() {

    var x = 3

    // address of a var
    fmt.Printf("%v\n", &x) // 0xc420014090

    // golang syntax
    fmt.Printf("%#v\n", &x) // (*int)(0xc420014090)

    // type of &x
    fmt.Printf("%T\n", &x) // *int
}

Get Pointer's Associate Variable's Value. (Dereferencing Pointer)

if x is a pointer, to get the value of the associated variable, use

*x

This is called “dereferencing pointer” or “pointer indirection”.

package main

import "fmt"

func main() {

    var x = 3

    var xp = &x
    fmt.Printf("%v\n", xp) // 0xc420014090

    var dxp = *xp
    fmt.Printf("%v\n", dxp) // 3

}

Example: Pointer to Struct

[see Golang: Struct]

package main

import "fmt"

type PP struct {
    x int
    y int
}

func main() {

    v := PP{1, 2}

    // get reference of v
    p := &v

    // set a field
    p.x = 999

    fmt.Printf("%#v\n", v) // main.PP{x:999, y:2}
}

Reference

The Go Programming Language Specification - The Go Programming Language#Pointer_types

The Go Programming Language Specification - The Go Programming Language#Address_operators

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Package, Import
  3. Comment
  4. Print
  5. String
  6. Rune
  7. Variable
  8. Zero Value
  9. Constant
  10. If Then Else
  11. Switch/Case
  12. Loop
  13. Basic Types
  14. Array
  15. Slice
  16. Map
  17. Struct
  18. Function
  19. regexp
  20. Read File
  21. Write to File
  22. Walk Dir
  23. Check File Exist
  24. System Call
  25. Pointer
  26. Defer
  27. Random Number

Examples

  1. Validate Links
  2. Generate Sitemap

Reference

  1. Go Spec