Skip to content

Instantly share code, notes, and snippets.

@devlights
Last active September 13, 2023 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devlights/5227a20352fe08f4d1588e1f024ee90f to your computer and use it in GitHub Desktop.
Save devlights/5227a20352fe08f4d1588e1f024ee90f to your computer and use it in GitHub Desktop.
Go で クロージャ (closure) のサンプル
$ go run main.go
1
2
3
4
5
package main
import "fmt"
func main() {
var (
closure = func(i int) func() int {
v := i
return func() int {
defer func() { v++ }()
return v
}
}
)
fn := closure(1)
for i := 0; i < 5; i++ {
fmt.Println(fn())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment