Literal

Function literals and closures

A function literal creates a function value and can refer to variables in enclosing lexical scopes. A captured variable is shared rather than automatically copied at closure creation. Example output: '1 2'.

Formal Syntax & Structure

go syntaxgo
func(parameters) results { body }

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  n := 0  next := func() int { n++; return n }  fmt.Println(next(), next())}
Go Reference