By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
In Go a function is a value, like an int or a string. You can store it in a variable, pass it to another function, and return it from a function. The name of a function without parentheses refers to the function itself. The same name with parentheses calls it.
package main
import "fmt"
func fee(cents int) int {
return cents + 35
}
func main() {
f := fee
fmt.Println(f(1000))
fmt.Println(fee(1000))
}f := fee copies the function value into f, so f(1000) and fee(1000) do the same work. The name without parentheses is how you talk about the function; the parentheses are how you run it.
Not every function needs a name. An anonymous function, also called a function literal, is written as func(...) { ... } in the middle of an expression. You can store it in a variable or call it straight away.
package main
import "fmt"
func main() {
greet := func(name string) {
fmt.Println("Hello,", name)
}
greet("Penny")
greet("Ada")
}Use a named function when several places call it. Use an anonymous function when the body is small and belongs exactly where it appears. The reader does not have to jump to another part of the file to understand it.
An anonymous function can read and change variables from the function where it was created. When it does, the combination is called a closure: the function closes over those variables and keeps them alive, even after the outer function has returned.
package main
import "fmt"
func counter() func() int {
count := 0
return func() int {
count++
return count
}
}
func main() {
next := counter()
fmt.Println(next())
fmt.Println(next())
fmt.Println(next())
other := counter()
fmt.Println(other())
}counter declares count := 0 and returns an anonymous function. Every time main calls next(), the anonymous function increments count and returns the new value. count survives between calls because the closure holds it. When counter returns, its local variables would normally be gone; this one is captured.
other := counter() creates a second, independent closure with its own count. That is why the last line prints 1: the second counter never saw the three calls made through the first one. The captured variables belong to the closure, not to the package and not to every copy of the function.
Closures are how you keep a running total without a package-level variable. The function below adds each amount to a total it remembers and returns the new total after every call.
package main
import "fmt"
func formatCents(cents int) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
func runningTotal() func(cents int) int {
total := 0
return func(cents int) int {
total += cents
return total
}
}
func main() {
add := runningTotal()
fmt.Println("after lunch:", formatCents(add(1250)))
fmt.Println("after coffee:", formatCents(add(450)))
fmt.Println("after bus:", formatCents(add(300)))
}The add function takes one amount per call, yet it can produce a running total because the closure keeps total between calls. State and behaviour travel together, and the rest of the program cannot reach the total by accident. Section 6 builds on this idea when methods carry state on a struct.
Write func adder(start int) func(int) int that returns a closure adding each argument to a total beginning at start, and returning the new total. Call it with adder(100), then pass 1250 and 99, printing each result with formatCents. Expected: $13.50 and then $14.49. Then create a second closure with adder(0) and confirm its first result is not affected by the first closure's calls.
Key takeaways
Function names without parentheses are values; adding () calls the function.
An anonymous function func(...) { ... } can be stored in a variable or called on the spot.
A closure captures variables from the surrounding function and keeps them alive after it returns.
Each closure created by a factory call has its own captured variables.
Closures keep related state and behaviour together without package-level variables.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.