Go is strictly call by value: every function argument is copied into the parameter. Go has no true call by reference. When you pass a pointer, the pointer itself is copied, but the copy still points to the same variable, so the function can change the original through it.
Slices, maps, channels, functions and interfaces are also passed by value, but they are small headers that refer to shared data. That is why changes to their contents can be visible to the caller, which people often mistake for call by reference.
When you pass an int, string, array or struct, the function works on its own copy. Assigning to the parameter never changes the caller's variable.
Arrays are copied entirely, so passing a large array by value is expensive and its changes are never seen by the caller. Structs behave the same way.
To let a function modify the caller's variable, pass its address with & and dereference with *.
The parameter x is a copy of the address, so reassigning x itself (for example x = nil) only affects the local copy. Only writing through *x changes v. Pointer parameters are also how methods with pointer receivers modify structs.
A slice value is a small header holding a pointer to an array, a length and a capacity. Passing a slice copies the header, not the elements, so writing to an element is visible to the caller. Growing the slice with append is not, because the header in the caller is unchanged.
To let a function grow a slice for the caller, return the new slice (nums = add(nums)) or pass a pointer to the slice.
Maps and channels are pointers to runtime structures under the hood, so adding keys to a map inside a function is visible to the caller. Reassigning the map variable itself (m = make(...)) is not.
| Type | Copied on call | Contents shared with caller |
|---|---|---|
int, float64, bool, string | Value | No |
| Array, struct | Entire value | No |
| Pointer | The address | Yes, the pointed-to value |
| Slice | Header (pointer, len, cap) | Yes, the elements |
| Map, channel | Reference to runtime data | Yes |
sync.Mutex.Passing a pointer does not automatically make code faster. It can force the value to escape to the heap, which adds garbage collection work. Check with go build -gcflags=-m and benchmark if performance matters.
The most frequent bug is modifying a copy by accident. Ranging over a slice of structs gives you a copy of each element, so changes to the loop variable do not reach the slice.
Another trap is a value receiver on a method that is meant to mutate. The method changes its copy and the caller sees nothing. Interfaces also hold a copy of the value stored in them, so storing a struct in an any and mutating the original later does not change what the interface holds.
Not as a language feature. Go always copies arguments. Pointers give you the same effect, because the copied pointer refers to the original variable.
The function received a copy of the slice header. Element writes go to the shared array, but append updates only the local header. Return the slice and reassign it in the caller.
A map value is a handle to runtime data, so inserts and deletes are visible to the caller. It is still passed by value, and reassigning the variable does not affect the caller.
Only when profiling shows a benefit or when you need mutation. Small structs are cheap to copy, and values avoid heap escapes.
Everything in Go is passed by value. Pointers, slices, maps and channels hold references to shared data, so changes made through them can be seen by the caller. Use pointers for mutation and for types that must not be copied, and plain values otherwise.