Files
go-koans/about_anonymous_functions.go
Tommy Parnell a7d91efcfc finished woo
2017-07-08 01:22:34 -04:00

30 lines
443 B
Go

package go_koans
func aboutAnonymousFunctions() {
{
i := 1
increment := func() {
i++
}
increment()
assert(i == 2) // closures function in an obvious way
}
{
i := 1
increment := func(x int) {
x++
}
increment(i)
assert(i == 1) // although anonymous functions need not always be closures
}
{
double := func(x int) int { return x * 2 }
assert(double(3) == 6) // they can do anything our hearts desire
}
}