Skip to content

Instantly share code, notes, and snippets.

@tylerlrhodes
Created July 8, 2018 18:36
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 tylerlrhodes/337559bf0892bc9fdeaf317baf70d89b to your computer and use it in GitHub Desktop.
Save tylerlrhodes/337559bf0892bc9fdeaf317baf70d89b to your computer and use it in GitHub Desktop.
Function Closures example in Go, from Tour of Go
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment