Learning Golang. Day 7

Learning Golang. Day 7

Here we are on day 7. Today, I started learning about closures!


Today was, admittedly, a tough one. Closures. What can I say about them. In some ways, a lot of ways, they make a lot of sense. However, in others, they feel a little like voodoo. If you’re not sure what I mean, take this quote from zetcode.com:

A Go closure is an anonymous nested function which retains bindings to variables defined outside the body of the closure. Closures can hold a unique state of their own. The state then becomes isolated as we create new instances of the function.

This quote requires further context, also from zetcode:

A nested function, also called an inner function, is a function defined inside another function. An anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions

So, if I understand it all correctly, a Go Closure is an anonymous function that has knowledge of the state outside it’s own definition, even after that outside context is destroyed. If so, I’m totally fine with needing some time to let this sink in, as I mull it over repeatedly.

In some ways, it seems to run totally counter to what I’m used to. However, I also appreciate the benefits of a closure’s functionality. I’m just struggling a little with them because the concept is still pretty new to me.

For complete transparency, I have worked with anonymous functions and closures in PHP, but not as much as I’d like to have.

Now, for a code example (of course!). The below code is my first attempt at solving the Fibonacci closure exercise in the Go Tour, heavily inspired by other solutions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package main

import "fmt"

func fibonacci() func() int {
	first, second := 0, 1
	return func() int {
		output := first + second
		first = second
		second = output
		return output
	}
}

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

If you run it, you’ll see the following output:

1
2
3
5
8
13
21
34
55
89

Technically, I believe this solution is wrong, or at the least, incomplete, because afaik a Fibonacci number should start with the first number (0) not the second (1). That said, in working through the solution, and just staring at it for what feels like the longest time, I do have a better appreciation of closures.

As the call to fibonacci() returns the closure, which in turn has reference to two variables, first and second, in the outer scope of the closure, then after the call to fibonacci() ends and its state is cleared up, the closure still has awareness of that state.

Given that, when it’s passed to fmt.Println() in the for loop, then it can work with that acquired state. Despite that, it still feels a little like magic. Not to worry, though. With time and more practice, I’m sure it will continue to sink in, and I’ll grow in my appreciation of closures. Now; to use them more in PHP.

See you, next time!


You might also be interested in...


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.


Join the discussion

comments powered by Disqus