Learning Golang (some rough notes) - S01E04 - Function Closures

Published by in Go, Golang, Function Closures at https://rmoff.net/2020/06/29/learning-golang-some-rough-notes-s01e04-function-closures/

So far the Tour has been 🤔 and 🧐 and even 🤨 but function closures had me 🤯 …

Each of the words on the page made sense but strung together in a sentence didn’t really make any sense to me.

Google resources threw up some nice explanations:

This one gets into some more hands-on examples

It also acted as a spoiler for the function closure exercise since that was the first example it gives :)

func fibonacci() func() int {
	f1 := 1
	f2 := 0
	return func() int {
		f1,f2 = f2, (f1+f2)
		return f1
	}
}

I tweaked the version that I’d seen so that the return values stated at zero

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181