Last active
February 3, 2026 20:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*?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