If Go Passes Everything by Value, How Do Pointers Change Anything?
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 short 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.
Go passes arguments by value
Consider a function that accepts a struct:
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 } When increment is called, Go copies counter into the parameter c. The function changes that copy, so the original remains 1.
Now change the parameter to a pointer:
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) // 2 } Go still passes the argument by value. This time, the value copied into c is an address. Both the caller and the function can use that address to access the same Counter.
That is why the function can change counter.Value.
A pointer receiver can modify the receiver
A method is a function with a receiver. The receiver can be a value or a pointer.
type Account struct { Balance int } func (a *Account) Deposit(amount int) { a.Balance += amount } Here, a is a pointer to an Account. Calling the method on a variable is simple:
account := Account{Balance: 100} account.Deposit(50) fmt.Println(account.Balance) // 150 Because account is addressable, Go allows the shorthand account.Deposit(50). You do not have to write (&account).Deposit(50).
The method still has a pointer receiver. Go’s shorthand does not change its declaration or turn it into a value receiver.
Value receivers work with a copy
A value receiver receives a copy of the value:
func (a Account) BalanceAfterFee(fee int) int { return a.Balance - fee } This method calculates a result without changing the original account. A pointer receiver is appropriate when a method needs to modify the receiver:
func (a *Account) Deposit(amount int) { a.Balance += amount } Mutation is one reason to use a pointer receiver, but it is not the only consideration. Copying a large struct may be undesirable. Some types also should not be copied after they have been used, such as types that contain synchronization primitives.
For consistency, Go code often uses the same receiver style for a type’s methods, especially when the type has a mix of methods that read and modify state.
Pointers and interfaces
Receiver choice also affects whether a type satisfies an interface.
type Depositor interface { Deposit(amount int) } type Account struct { Balance int } func (a *Account) Deposit(amount int) { a.Balance += amount } The Deposit method has a pointer receiver. A *Account has that method in its method set, so it can satisfy Depositor. A plain Account value does not have the pointer-receiver method in its method set.
For example:
var d Depositor account := Account{Balance: 100} d = &account // valid Assigning account without & would not satisfy this interface because Account does not have the required method in its method set.
This is separate from the convenience of calling account.Deposit(50). Go may automatically take the address of an addressable variable for a method call, but it does not automatically convert a value into a pointer when checking interface satisfaction.
A quick mental model
When reading a function or method, check the type of the parameter or receiver:
Accountmeans the function receives a copy of the struct.*Accountmeans it receives a copy of a pointer to the struct.- Changes made through that pointer can affect the struct it points to.
- A pointer-receiver method belongs to the method set of
*Account, not plainAccount.
Go does not pass the original variable “by reference.” It passes arguments by value. With a pointer argument, the copied value is an address that can still refer to the same underlying data.
That distinction makes pointer and receiver behaviour much easier to reason about, especially when debugging unexpected mutations or interface errors.