anonymous functions
This commit is contained in:
29
about_anonymous_functions.go
Normal file
29
about_anonymous_functions.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package go_koans
|
||||
|
||||
func testAnonymousFunctions() {
|
||||
{
|
||||
i := 1
|
||||
increment := func() {
|
||||
i++
|
||||
}
|
||||
increment()
|
||||
|
||||
assert(i == __int__) // closures function in an obvious way
|
||||
}
|
||||
|
||||
{
|
||||
i := 1
|
||||
increment := func(x int) {
|
||||
x++
|
||||
}
|
||||
increment(i)
|
||||
|
||||
assert(i == __int__) // although anonymous functions need not always be closures
|
||||
}
|
||||
|
||||
{
|
||||
double := func(x int) int { return x * 2 }
|
||||
|
||||
assert(double(3) == __int__) // they can do anything our hearts desire
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,12 @@ func TestKoans(t *testing.T) {
|
||||
//testArrays()
|
||||
//testSlices()
|
||||
//testControlFlow()
|
||||
testEnumeration()
|
||||
//testEnumeration()
|
||||
testAnonymousFunctions()
|
||||
|
||||
// TODO: ie, gameplan
|
||||
//testFiles()
|
||||
//testVariadicFunctions()
|
||||
//testAnonymousFunctions()
|
||||
//testInterfaces()
|
||||
//testMaps()
|
||||
//testStructs()
|
||||
|
||||
Reference in New Issue
Block a user