DEV Community

Cover image for Go Pointers and Method Receivers, Without the “Pass by Reference” Confusion

Go Pointers and Method Receivers, Without the “Pass by Reference” Confusion

Go Pointers and Method Receivers, Without the “Pass by Reference” Confusion

Pointers and method receivers can be confusing when you are learning Go. A common question is: if Go passes arguments by value, how can a method change a struct?

The answer is that a pointer is also a value. Passing a pointer copies the pointer, not the data it points to. The original pointer and the copy can both refer to the same value.

Passing a struct copies it

Here is a function that takes a Counter value:

package main

import "fmt"

type Counter struct {
    Value int
}

func increment(c Counter) {
    c.Value++
}

func main() {
    counter := Counter{Value: 1}
    increment(counter)

    fmt.Println(counter.Value) // 1
}
Enter fullscreen mode Exit fullscreen mode

The parameter c is a copy of counter. The function changes that copy, so the original value stays at 1.

Now change the parameter to a pointer:

func increment(c *Counter) {
    c.Value++
}
Enter fullscreen mode Exit fullscreen mode

When called with increment(&counter), the function receives a copy of the address. That address still points to the same Counter, so the function can change its Value.

Go still passed the argument by value. The value it copied was a pointer.

Pointer receivers can modify a value

A method is a function associated with a receiver. A pointer receiver can modify the value it points to:

type Account struct {
    Balance int
}

func (a *Account) Deposit(amount int) {
    a.Balance += amount
}
Enter fullscreen mode Exit fullscreen mode

You can call this method on an addressable variable without writing the address explicitly:

account := Account{Balance: 100}
account.Deposit(50)

fmt.Println(account.Balance) // 150
Enter fullscreen mode Exit fullscreen mode

Go allows this shorthand because account is addressable. The method declaration still has a pointer receiver.

A value receiver works with a copy:

func (a Account) BalanceAfterFee(fee int) int {
    return a.Balance - fee
}
Enter fullscreen mode Exit fullscreen mode

Use a pointer receiver when the method needs to change the receiver. Copying behaviour, the size of the struct and consistency with the type’s other methods can also influence the choice.

Pointer receivers and interfaces

Receiver choice also affects interface implementation:

type Depositor interface {
    Deposit(amount int)
}

type Account struct {
    Balance int
}

func (a *Account) Deposit(amount int) {
    a.Balance += amount
}
Enter fullscreen mode Exit fullscreen mode

Because Deposit has a pointer receiver, *Account has that method in its method set. A pointer to an account can satisfy Depositor:

var d Depositor

account := Account{Balance: 100}
d = &account
Enter fullscreen mode Exit fullscreen mode

A plain Account value does not satisfy the interface in this example. Go’s shorthand for calling a method on an addressable variable does not change the rules for interface satisfaction.

A simple mental model

When you see a parameter or receiver, check whether it is a value or a pointer.

Account means the function receives a copy of the struct. *Account means it receives a copy of a pointer to the struct. Changes made through that pointer can affect the original value.

Go does not pass the original variable “by reference.” It passes arguments by value, and a pointer value still contains an address that can refer to shared data. Keeping those facts separate makes pointer behaviour and interface errors easier to understand.

Top comments (0)