Go treats functions as first-class values, so a function can take another function as an argument, return a function, or both. A function that does either is called a higher-order function. You use them for callbacks, middleware, filters, retries and configuration, and since Go 1.18 generics make them type-safe and reusable.
The rest of this article shows the core patterns with complete programs, then covers closures, generic helpers, the standard library's own higher-order APIs and the pitfalls to avoid.
A function's type is its signature, for example func(int) int. You can assign a function to a variable, store it in a slice or map, and pass it around like any other value. Giving the signature a name makes code easier to read:
apply does not know what the operation does. The caller decides, either with a named function or an anonymous function literal.
Callbacks are the most common use. The caller supplies behaviour, and the higher-order function supplies the surrounding logic such as iteration, error handling or timing.
The wrapper times any operation without knowing what it does. Functions that take a callback are also how sort.Slice, http.HandleFunc and strings.Map work.
A function can build and return another function. The returned function keeps access to the variables of the scope that created it. This is a closure.
Each call to counter creates a new n, so two counters never share state. Closures are not goroutine-safe by themselves: if several goroutines call the same counter, guard n with a sync.Mutex or use sync/atomic.
Since Go 1.22, each iteration of a for loop has its own copy of the loop variable, so closures created inside a loop capture the value of that iteration. In older versions you had to copy the variable (i := i) to get the same result.
Before generics, Map and Filter needed interface{} and type assertions. With type parameters they are type-safe:
The compiler infers T and U from the arguments, so the call sites stay short. Go does not need to ship these helpers in the standard library because a plain for loop is usually just as clear, so write them when they remove real repetition.
You already use higher-order functions in everyday Go:
| API | Function parameter |
|---|---|
sort.Slice(s, less) | less func(i, j int) bool |
slices.SortFunc(s, cmp) | cmp func(a, b E) int |
slices.IndexFunc(s, f) | predicate func(E) bool |
strings.Map(mapping, s) | mapping func(rune) rune |
http.HandlerFunc | adapts a function to the http.Handler interface |
sync.OnceFunc(f) | returns a function that runs f only once |
The slices and maps packages (Go 1.21 and later) cover most of the common cases, and Go 1.23 added iterator functions of type iter.Seq, which are themselves higher-order: a sequence is a function that accepts a yield callback.
Two idioms in real Go code are built on returned functions. HTTP middleware wraps a handler and returns a new one:
The functional options pattern uses type Option func(*Server) so a constructor like NewServer(addr, WithTimeout(5*time.Second)) accepts optional settings without a long parameter list.
if fn != nil when a callback is optional.nil, so you cannot use them as map keys or test two functions for equality.Yes. Functions are first-class values with a type, so they can be parameters, return values, struct fields and map values.
A higher-order function takes or returns a function. A closure is a function value that captures variables from its surrounding scope. A function that returns a closure is both.
A call through a function value cannot always be inlined, and a captured variable may be moved to the heap, so there is a small cost. In most programs it is negligible, and you should measure with go test -bench before optimising.
Only when it removes repeated code. A for loop is idiomatic and fast, and the slices package already offers many helpers.
Higher-order functions in Go are ordinary functions that accept or return other functions. Use them for callbacks, closures that carry state, generic helpers such as Map, Filter and Reduce, middleware, and functional options. Remember that closures capture variables by reference, guard shared state in concurrent code, and prefer a plain loop when it is clearer.