Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:02
Show Gist options
  • Save dacr/769f2fb70d367f0848946db652f445aa to your computer and use it in GitHub Desktop.
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/4519afe9647f8a6a7ee3ce70c2ee3c12ae353208
/*?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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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