Go Reference ยท Keywords and Statements
The func keyword
Reference for Go func: declaring functions, multiple results, variadic parameters, methods with receivers, function literals, closures and function types.
func declares functions, methods, function literals (anonymous functions) and function types. Functions are first-class values: you can store them in variables, pass them as arguments, return them and compare them to nil. A function literal that refers to variables of an enclosing function is a closure and keeps those variables alive.
A function can return several values, which is how Go reports errors. Parameters are always passed by value, though slices, maps, channels, pointers and functions are small headers that refer to shared data.
func add(a, b int) int { return a + b }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func name(p1 T1, p2 T2) (R1, R2) { ... } func name(a, b T) R { ... } func (r Receiver) name(params) results { ... } func (r *Receiver) name(params) results { ... } func name[T any](p T) T { ... } func(params) results { ... } // literal type Handler func(string) error // function type
| Part | Description |
|---|---|
name |
Function name. main and init have special meaning in packages. |
| parameters | name Type pairs. Consecutive parameters of one type can share it. The last may be variadic: rest ...T, seen inside as []T. |
| results | One type, or a parenthesized list. Results may be named, which declares them as variables initialized to zero values. |
| receiver | Makes the function a method on a defined type or pointer to one. |
| type parameters | [T any] makes the function generic (Go 1.18+). |
Errors and edge cases:
missing return: every path of a function with results must end in a return, a panic, or an infinite loop.- Methods can be declared only on types defined in the same package; not on
intor[]string, and not on pointer or interface base types. - Go has no overloading and no default parameter values; two functions in one package cannot share a name.
- A function with no body (
func f()without braces) declares one implemented elsewhere, such as in assembly.
Usage notes
- Pass by value: the callee gets copies of its arguments. Modify the caller's value through a pointer parameter, or through a slice/map that shares storage.
- Variadic functions accept zero or more arguments; calling with
s...passes an existing slice without copying it, so the callee can modify its elements. - Closures capture variables, not values. Since Go 1.22 each loop iteration has its own copy of loop variables, so closures created in a
forloop capture a per-iteration value. - Functions are not comparable except to
nil, and so cannot be map keys. - Method values and expressions:
c.Incis a bound function value;(*Counter).Incis a function taking the receiver first. - Recursion is supported; Go grows the goroutine stack as needed (up to 1 GB by default on 64-bit systems).
- Order of evaluation: arguments are evaluated left to right before the call;
deferruns after the function body finishes.