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. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec