How to Pass a Function as an Argument in Go
In Go, functions are values: you can store one in a variable and pass it to another function. The receiving parameter's type describes the function it accepts, such as func(int) int, a function that takes an int and returns an int.
Syntax
func <name>(<value> <type>, <op> func(<paramTypes>) <resultType>) <resultType> {
return <op>(<value>)
}
<name>(<value>, func(<param> <type>) <resultType> { return ... })
| Part | What it does |
|---|---|
func(<paramTypes>) <resultType> | A function type: what the passed function must take and return. |
<op>(<value>) | Calls the function that was passed in. |
func(...) ... { ... } | A function literal: a function written in place, without a name. |
The Go specification: "A function literal represents an anonymous function." It adds: "A function literal can be assigned to a variable or invoked directly."
The Example
The example writes apply, which calls whatever function it receives on a number, and passes it a doubling function and a squaring one.
Prerequisites
- Go.
go runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named main.go:

package main
import "fmt"
func apply(value int, op func(int) int) int {
return op(value)
}
func main() {
double := func(n int) int { return n * 2 }
fmt.Println(apply(21, double))
fmt.Println(apply(5, func(n int) int { return n * n }))
}
doubleholds a function literal; passingdoublepasses the function, not its result.- The second call writes the function in place, right in the argument list.
Step 2: Run It
go run main.go
42
25

The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Passing a function of another type | cannot use func(s string) string {…} (value of type func(s string) string) as func(int) int value in argument to apply | Match the parameter and result types exactly |
Writing apply(21, double(21)) | That passes double's result, an int, not the function | Pass double without brackets |
Calling a nil function parameter | A run-time panic | Check op != nil when callers may omit it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot use … as func(int) int value | The function's signature differs | Change the function or the parameter type |
panic: runtime error: invalid memory address or nil pointer dereference at the call | op was nil | Pass a function, or guard the call |
FAQ
Can a function literal use variables from around it?
Yes. Function literals are closures: they can read and change variables of the function they're written in.
Can I give a function type a name?
Yes: type Operation func(int) int, then func apply(value int, op Operation) int.
Conclusion
Give a parameter a function type such as func(int) int, then pass a function variable or write a function literal in place. The types must match exactly.
Additional Resources
- Function literals, in the Go specification.
- Function types, in the Go specification.
