In Go, a function receives data through typed parameters and sends data back through one or more return values. Arguments are always passed by value, so the function gets a copy, but copying a pointer, slice, map, channel or interface still lets the function reach the shared underlying data. Functions can return several results, named results and, by convention, an error as the last value.
This article covers the parameter and return forms you will use daily and the pass-by-value rules that decide whether a change is visible to the caller.
Parameters are written as name type. Adjacent parameters of the same type can share the type, and the return types follow the parameter list.
Go has no default arguments, no optional parameters and no overloading. To get optional behaviour use a config struct or functional options, described below.
A function can return any number of values, which is how Go reports failure without exceptions. The caller must receive or explicitly discard each one.
Use _ to ignore a value you do not need, for example q, _, err := divmod(17, 5). Ignoring an error that way should be rare and deliberate.
Return values can be named. Named results start at their zero value and act like local variables, and a bare return sends their current values back.
The built-in min and max exist since Go 1.21. Named results are most useful for documenting what each value means and for deferred functions that adjust the result, such as turning a panic into an error. Avoid naked returns in long functions because readers must scan back to see what is returned.
Every argument is copied. What the copy refers to decides what the caller sees.
| Argument type | Change to the parameter visible to caller? |
|---|---|
int, string, bool, float64, arrays, structs | no, the whole value is copied |
| pointer | the pointed-to value yes, reassigning the pointer no |
| slice | element changes yes, append growth not unless you return the slice |
| map, channel | yes, they refer to shared data |
| interface | depends on the dynamic value stored inside |
Use a pointer parameter when the function must modify a struct or when the struct is large. For small structs, passing by value is simple and often just as fast.
A final parameter written ...T accepts zero or more values and is a []T inside the function. Spread an existing slice with ... at the call site.
Functions are values, so a parameter can itself be a function, which is the basis of callbacks and functional options:
No. Go always passes by value. Passing a pointer copies the pointer, which lets the callee modify the value it points to.
append may return a new slice header with a different length. Return the result and assign it in the caller.
No. Use a config struct, variadic options or functional options instead.
Use them for short functions where the names document the results, or when a deferred function needs to change the result. Skip them otherwise.
Go functions take typed parameters by value and return any number of results, with error last by convention. Use pointers when a function must change the caller's data, return updated slices from append, prefer explicit returns over naked ones, and use variadic parameters or functional options instead of defaults.