Functions
A function is a named block of code with a typed parameter list and a typed result list. Functions are values: you can assign them, pass them, and return them.
Detailed Explanation
Almost everything you call in Go is a function: fmt.Println, os.Open, the NewServer you wrote this morning. A method is a function with a receiver. A closure is a function that refers to names from an outer function. The language treats all of them as values of function type.
Signature is the type. func load(path string) ([]byte, error) has type func(string) ([]byte, error). Two functions are assignable when their parameter and result types match. Names in the signature are documentation; func(p string) ([]byte, error) is the same type.
Calls copy arguments. A string argument copies the header. A struct argument copies the struct. A slice argument copies the slice header, which still points at the same array, so the callee can change elements but not the caller's header unless it returns a new slice. There is no ref keyword. If you want the caller to see a changed struct, take a pointer.
Multiple results are ordinary. Returning (T, error) is the standard way to say "value or failure." The caller assigns both. Ignoring the error with _ compiles and is usually a bug.
First-class and anonymous. fn := strings.TrimSpace stores a function. fn := func(s string) string { return s + "\n" } writes one without a package-level name. HTTP handlers, sort.Slice comparators, and errgroup tasks are all functions you pass in.
There is no default argument list and no keyword arguments. If you need optional configuration, use a struct of options, a functional-options loop, or several small functions (Listen, ListenTLS) instead of one function with ten parameters.
Recursion is allowed. There is no tail-call guarantee. Deep recursion on a large input can overflow the stack; prefer an explicit slice as a stack for that case.
What it costs. A call is cheap. A call that copies a 2 KiB struct on every request is not. A function literal that captures a large local can keep that local alive longer than you expect, which is an escape-analysis and leak question, not a syntax one.
Code Examples
package main
import "fmt"
func greet(name string) string {
return "hello " + name
}
func main() {
fn := greet
fmt.Println(fn("shop"))
}func load(path string) ([]byte, error) {
if path == "" {
return nil, fmt.Errorf("empty path")
}
return []byte(path), nil
}func apply(s string, fn func(string) string) string {
return fn(s)
}Common Gotchas
Arguments are copied
a callee that mutates a struct parameter does not change the caller. Take `*T` when mutation is the point.
Discarding `error` with `_` compiles
linters catch some cases; reviewers should catch the rest.
Parameter names are not part of the type
you can implement `func(string) error` with any parameter name.
There is no function overloading
two functions in one package cannot share a name with different signatures. Use different names or a common interface.