Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:22
Show Gist options
  • Select an option

  • Save dacr/769f2fb70d367f0848946db652f445aa to your computer and use it in GitHub Desktop.

Select an option

Save dacr/769f2fb70d367f0848946db652f445aa to your computer and use it in GitHub Desktop.
go closures / published by https://github.com/dacr/code-examples-manager #0fe71bda-9959-438c-adf7-4b53c50fea24/a4db48d4d227551ef41eaf6d5d7488892353f831
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/
// summary : go closures
// keywords : go, closures, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 0fe71bda-9959-438c-adf7-4b53c50fea24
// created-on : 2025-03-27T10:08:08+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : nix-shell -p go --run "go run $file"
package main
import "fmt"
func main() {
makeIncrementer := func(start, step int) func() int {
current := start
return func() int {
current += step
return current
}
}
inc := makeIncrementer(1, 2)
fmt.Println(inc())
fmt.Println(inc())
fmt.Println(inc())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment