$ go run main.go
1
2
3
4
5
Last active
September 13, 2023 15:30
-
-
Save devlights/5227a20352fe08f4d1588e1f024ee90f to your computer and use it in GitHub Desktop.
Go で クロージャ (closure) のサンプル
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
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