/* * Go allows functions to be defined within functions. * That is all that is happening here * @author gtowell * created July 20, 2021 */ package main import "fmt" // main is defined as usual for functions func main() { a(24) aa(5) c() } //the function a has a function defined within in. //the function a is public, only a can see its internal func func a(aa int) { aaa := aa * 2 // define a function and store it in the valuable b // cannot use the "func b(bb int)" syntax for internal funcs // this cannot be recursive b := func(bb int) { bbb := bb * 10 aaa = aaa * 2 // what happens when change = to := fmt.Printf("B: aa=%v aaa=%v bb=%v bbb=%v\n", aa, aaa, bb, bbb) } b(20) fmt.Printf("A: aa=%v aaa=%v\n", aa, aaa) } func aa(aa int) { aaa := aa * 2 // define a function and store it in the valuable b // cannot use the "func b(bb int)" syntax for internal funcs // this cannot be recursive var b func(int) int b = func(bb int) int { if bb < 0 { return 10 } fmt.Printf("aa_b: aa=%v aaa=%v bb=%v\n", aa, aaa, bb) bb = b(bb-1) + 5 fmt.Printf("aa_b bb: aa=%v aaa=%v bb=%v\n", aa, aaa, bb) return bb } fmt.Printf("aa_b %d\n", b(aaa)) fmt.Printf("A: aa=%v aaa=%v\n", aa, aaa) } // cannot use := for static assignment in global space!! // for functions or anything else // so c := func() {} is not allowed (!) // but can use the var style assignment var c = func() { fmt.Println("C") }