search

What is the difference between Go's pass by value and pass by reference for function arguments?

In Go, all function arguments are passed by value, including pointers. When a value is passed to a function, a copy of the value is created and passed to the function. This means that changes made to the value within the function are not reflected outside the function.

However, when a pointer is passed to a function, a copy of the pointer value is created and passed to the function. This copy still points to the original data, so changes made to the data through the pointer within the function are visible outside the function.

In this sense, Go uses pass by value semantics for all function arguments, but allows passing pointers to enable pass by reference semantics.

Related Questions You Might Be Interested