Value Functions

Passing a function about

func main() {
	fn := hello

	printString("bob", fn)
}

func printString(s string, fn func(string) string) {
	fmt.Println(fn(s))
}

func hello(name string) string {
	return fmt.Sprintf("Hello %v", name)
}

Closures

A closure is when a method references a value outside its scope

func fibonacci() func() int {
	a := 0
	prev := 1
	
	return func() int {
		result := a
		
		temp := a
		a += prev
		prev = temp

		return result
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

/*
Output:
	0
	1
	1
	2
	3
	5
*/