Tutorial: Go (Golang) Pointers in 5 Minutes

Post to Twitter

In my previous Go articles I showed you how to setup Go on Ubuntu as well as how to create a couple different REST projects (here and here). Today, I’m going to go over how to use pointers and references in Go.


I’m assuming you already have Go up and running, if not make sure to do that first.

Let’s start with a very simple application that simply takes a value and runs a calculation on it.

Create a new file and call it: example.go

package main

import "fmt"

func updateValue(val int) {
    val = val + 100
}

func main() {
    val := 1000
    updateValue(val)
    fmt.Println("val:", val)
}

If you run this code like so:

$ go run example.go

The results will be:

1000

You might have expected the output to have been 1100 however you need to keep in mind that by default when you pass an argument to a function in Go it will be copied by value. This simply means a “copy” of the val variable is used by the updateValue function.

Let’s fix this so that updateValue will change the val value. In order to do this we need to pass the val variable by reference. That is, we will pass in the address of the val variable and then updateValue can change that value.

Here is the new code for the example.go file:

package main

import "fmt"

func updateValue(someVal *int) {
    *someVal = *someVal + 100
}

func main() {
    val := 1000
    updateValue(&val)
    fmt.Println("val:", val)
}

The results will be:

1100

You can see here I’ve used an asterix (Pointer) * and an ampersand (Reference) &. The updateValue function has been updated to accept an integer (someVal) that is a pointer. A pointer points to a memory address location and allows us to change the value. When we call the updateValue function now we need to send the reference (address) of the val variable. You can see in the updateValue function I don’t even need to name the variable val, I can call it whatever I want because its just a pointer.

Go also has a nifty function called new. When using the new function you pass in the type, like int, float32, etc. What new will return back to you is a pointer to a location in memory set aside that can hold the type you specified. A benefit of using new is you can simply pass the variable (pointer) without having to use the & operator.

package main

import "fmt"

func updateValue(someVal *int, someVal2 *float64) {
    *someVal = *someVal + 100
    *someVal2 = *someVal2 + 1.75    
}
func main() {
    val := 1000
    val2 := new(float64)
    updateValue(&val, val2)
    fmt.Println("val:", val)
    fmt.Println("val2:", *val2)    
}

The results will be:

val: 1100
val2: 1.75

Let’s kick this up a bit and add in a struct. We will create a struct to hold a stock’s high, low and closing market price.

Here is the code:

package main

import "fmt"

type Stock struct {
    high float64
    low float64
    close float64
}

func main() {
    goog := Stock{454.43, 421.01, 435.29}
    fmt.Println("Original Stock Data:", goog)
}

The results will be:

Original Stock Data: {454.43 421.01 435.29}

Easy enough. Now, we will modify the contents of the stock struct.

package main

import "fmt"

type Stock struct {
    high float64
    low float64
    close float64
}

func modifyStock(stock *Stock) {
    stock.high = 475.10
    stock.low = 400.15
    stock.close = 450.75    
}

func main() {
    goog := Stock{454.43, 421.01, 435.29}
    fmt.Println("Original Stock Data:", goog)
    modifyStock(&goog)
    fmt.Println("Modified Stock Data:", goog)    
}

The results will be:

Original Stock Data: {454.43 421.01 435.29}
Modified Stock Data: {475.1 400.15 450.75}

Post to Twitter

This entry was posted in Go, Open Source. Bookmark the permalink.

2 Responses to Tutorial: Go (Golang) Pointers in 5 Minutes

  1. @_@ says:

    easy & simple thanks.

  2. Gabriel says:

    Thanks, for this series of articles!! ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>