Skip to content
Justice Tech School

Function Values and Closures

Functions are values. A closure keeps the variables it uses.

Example
package main

import "fmt"

func counter() func() int {
	n := 0
	return func() int {
		n++
		return n
	}
}

func main() {
	next := counter()
	fmt.Println(next(), next(), next())
}

Result

1 2 3

Exercise

Return a function.

func counter()  int

Quiz Test

What does a closure keep?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.