Skip to main content

How to Pass a Function as an Argument in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
func <name>(<value> <type>, <op> func(<paramTypes>) <resultType>) <resultType> {
return <op>(<value>)
}

<name>(<value>, func(<param> <type>) <resultType> { return ... })
PartWhat 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 run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which defines apply(value int, op func(int) int) returning op(value), stores a doubling function literal in double, and prints apply(21, double) and apply(5, a squaring function literal).

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 }))
}
  • double holds a function literal; passing double passes the function, not its result.
  • The second call writes the function in place, right in the argument list.

Step 2: Run It​

Terminal
go run main.go
Output
42
25

A zsh terminal where go run main.go prints 42 and 25.

The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Passing a function of another typecannot use func(s string) string {…} (value of type func(s string) string) as func(int) int value in argument to applyMatch the parameter and result types exactly
Writing apply(21, double(21))That passes double's result, an int, not the functionPass double without brackets
Calling a nil function parameterA run-time panicCheck op != nil when callers may omit it

Troubleshooting​

SymptomLikely causeFix
cannot use … as func(int) int valueThe function's signature differsChange the function or the parameter type
panic: runtime error: invalid memory address or nil pointer dereference at the callop was nilPass 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​