Last active
June 23, 2018 06:00
-
-
Save broccoli1002/39e062765ca71a3ec68e8fe1eea9077d to your computer and use it in GitHub Desktop.
スターティングGO言語: クロージャーとしての関数 p105
This file contains 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 later() func(string) string { | |
// 1つ前に与えられた文字列を保存する変数 | |
var store string | |
// 引数に文字列をとり文字列を返す関数を返す | |
return func(next string) string { | |
s := store | |
store = next | |
return s | |
} | |
} | |
func main() { | |
f := later() | |
fmt.Println(f("Golang")) // => "" | |
fmt.Println(f("is")) // => "Golang" | |
fmt.Println(f("awesome!")) // => "is" | |
} | |
// store変数はL12のクロージャーに補足されているので破棄されない |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment