loading...

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

rmoff profile image Robin Moffatt Originally published at rmoff.net on ใƒป2 min read

๐Ÿ‘‰ A Tour of Go : 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 

Posted on by:

rmoff profile

Robin Moffatt

@rmoff

Robin Moffatt is a Developer Advocate at Confluent, and regular conference speaker. He also likes writing about himself in the third person, eating good breakfasts, and drinking good beer.

Discussion

markdown guide