Skip to content

Instantly share code, notes, and snippets.

@broccoli1002
Last active June 23, 2018 06:00
Show Gist options
  • Save broccoli1002/39e062765ca71a3ec68e8fe1eea9077d to your computer and use it in GitHub Desktop.
Save broccoli1002/39e062765ca71a3ec68e8fe1eea9077d to your computer and use it in GitHub Desktop.
スターティングGO言語: クロージャーとしての関数 p105
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