Created
July 8, 2018 18:36
-
-
Save tylerlrhodes/337559bf0892bc9fdeaf317baf70d89b to your computer and use it in GitHub Desktop.
Function Closures example in Go, from Tour of Go
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 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