Every time you call a function, the language has to decide what to give it. Does the function receive the value itself, or a way to reach the original? That choice (call by value or call by reference) determines whether changes inside the function are visible to the caller. Go is strictly call by value, always, but maps and slices make it look otherwise, and that trips up developers coming from other languages.
Call by value
When a language uses call by value, it passes a copy of the argument to the function. The function works with its own private copy, and anything it does to that copy has no effect on the original variable in the caller.
func double(n int) { n = n * 2 fmt.Println("inside:", n) // inside: 20}func main() { x := 10 double(x) fmt.Println("outside:", x) // outside: 10}double receives a copy of x. Multiplying that copy by two changes nothing in main. When the function returns, the copy is discarded and x is untouched.
Call by value is the default for primitive types in most languages: integers, floats, booleans, and similar. It is simple to reason about: the caller retains full ownership of its variables, and no function can surprise you by modifying something it was only meant to read.
Call by reference
When a language uses call by reference, it passes the memory address of the argument. Instead of a copy, the function gets direct access to the same memory the caller is using. Any modification the function makes is immediately visible in the caller.
Languages like C++ support call by reference explicitly. Java and Python use a form of it for objects: the reference (the address of the object) is copied, not the object itself. We will come back to this with Go.
The difference in practice:
| Call by value | Call by reference | |
|---|---|---|
| What is passed | A copy of the value | The memory address |
| Changes inside function | Affect only the local copy | Affect the original variable |
| Typical use | Primitive types | Large or shared data structures |
Go: strictly call by value
Go does not have call by reference. Every argument you pass to a function is copied. Even when you pass a pointer, the pointer itself is copied. The function receives its own copy of the address, not a reference to the pointer variable.
func tryToNilPointer(p *int) { p = nil // only the local copy of the pointer is set to nil}func main() { x := 42 ptr := &x tryToNilPointer(ptr) fmt.Println(ptr) // 0xc0000b4000 — still pointing to x fmt.Println(*ptr) // 42}The function changes its local copy of p to nil, but the original ptr in main is unaffected. Two copies of the pointer existed inside the call, and modifying one does not touch the other.
However, if both copies of the pointer hold the same address, both can be used to modify the value at that address:
func increment(p *int) { *p = *p + 1 // writes to the address both copies share}func main() { x := 0 increment(&x) fmt.Println(x) // 1}So Go is strictly call by value, but a value can be an address. Passing a pointer passes a copy of the address, and that copy still points to the same place.
Maps
Maps in Go behave in a way that surprises many newcomers: changes made to a map inside a function are visible to the caller, even though Go is call by value. To see why, look at what a map holds.
A map value is a pointer. make (or a map literal) allocates the hash table where the key-value pairs live and gives you a pointer to it, which is why a map variable takes only 8 bytes on a 64-bit machine. When you pass a map to a function, Go copies that pointer, and the copy points to the same hash table.
func addKey(m map[string]int) { m["new"] = 99}func main() { scores := map[string]int{"alice": 10} addKey(scores) fmt.Println(scores) // map[alice:10 new:99]}Both main and addKey hold a copy of the same pointer, so both reach the same underlying storage. Inserting a key inside the function writes to that shared storage, so the caller sees the change.
Slices
Slices follow the same pattern as maps, with one important complication. A slice header contains three fields: a length, a capacity, and a pointer to the backing array. When you pass a slice to a function, Go copies this header. The copy shares the same backing array.
Modifying an existing element through the local copy writes to the shared backing array and is visible to the caller:
func zeroFirst(s []int) { s[0] = 0}func main() { nums := []int{1, 2, 3} zeroFirst(nums) fmt.Println(nums) // [0 2 3]}But append is different. When append needs more capacity than the current backing array provides, it allocates a new array, copies the existing elements, and returns a new slice header pointing to the new array. The original backing array in the caller is unaffected.
func appendItem(s []int) { s = append(s, 99) fmt.Println("inside:", s) // inside: [1 2 3 99]}func main() { nums := []int{1, 2, 3} appendItem(nums) fmt.Println("outside:", nums) // outside: [1 2 3]}The function's local copy of the header gets updated to point to the new array, but main's header still points to the original. The 99 is lost when the function returns.
If you need a function to grow a slice and have the caller see the result, return the new slice and assign it at the call site:
func appendItem(s []int) []int { return append(s, 99)}func main() { nums := []int{1, 2, 3} nums = appendItem(nums) fmt.Println(nums) // [1 2 3 99]}Putting it together
Go's call-by-value rule has no exceptions. Maps and slices only look like references because a map is a pointer and a slice header contains one, both pointing to shared storage:
| Type | What is copied | Mutations visible to caller? | Reassignment visible? |
|---|---|---|---|
int, bool, string | The value itself | No | No |
Pointer (*T) | The address | Yes (via *p) | No |
| Map | Pointer to the shared table | Yes | No |
| Slice | Header (len, cap, pointer) | Yes (index writes) | No |
Slice + append (grows) | Header; new array allocated | No | No |
Go always copies the arguments. When the copy contains an address, the function can modify what that address points to, but never the caller's variable itself.