loading...

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

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

Learning Go (11 Part Series)

1) Learning Golang (some rough notes) - S01E00 2) Learning Golang (some rough notes) - S01E01 - Pointers 3 ... 9 3) Learning Golang (some rough notes) - S01E02 - Slices 4) Learning Golang (some rough notes) - S01E03 - Maps 5) Learning Golang (some rough notes) - S01E04 - Function Closures 6) Learning Golang (some rough notes) - S01E05 - Interfaces 7) Learning Golang (some rough notes) - S01E06 - Errors 8) Learning Golang (some rough notes) - S01E07 - Readers 9) Learning Golang (some rough notes) - S01E08 - Images 10) Learning Golang (some rough notes) - S01E09 - Concurrency (Channels, Goroutines) 11) Learning Golang (some rough notes) - S01E10 - Concurrency (Web Crawler)

๐Ÿ‘‰ 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 

Learning Go (11 Part Series)

1) Learning Golang (some rough notes) - S01E00 2) Learning Golang (some rough notes) - S01E01 - Pointers 3 ... 9 3) Learning Golang (some rough notes) - S01E02 - Slices 4) Learning Golang (some rough notes) - S01E03 - Maps 5) Learning Golang (some rough notes) - S01E04 - Function Closures 6) Learning Golang (some rough notes) - S01E05 - Interfaces 7) Learning Golang (some rough notes) - S01E06 - Errors 8) Learning Golang (some rough notes) - S01E07 - Readers 9) Learning Golang (some rough notes) - S01E08 - Images 10) Learning Golang (some rough notes) - S01E09 - Concurrency (Channels, Goroutines) 11) Learning Golang (some rough notes) - S01E10 - Concurrency (Web Crawler)

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