ProgrammingTechniques in Go: Closures

Guy J Grigsby
Star Gazers
Published in
3 min readSep 11, 2021

--

Golang Gopher with Lamda on their chest
The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
gopher.{ai,svg,png} was created by Takuya Ueda (https://twitter.com/tenntenn). Licensed under the Creative Commons 3.0 Attributions license.

This is the first article in a series about basic programming techniques as illustrated in Go. Recently I was explaining closures to a colleague who was a recent CS grad. They were not really clear on closures in general. It occurred to me that for folks who work mostly in object oriented languages, some functional techniques may not be super clear. I figured some articles about that could help some people. In addition, I struggled trying to define a closure presisely without examples. I wanted to be able to do that. Recently I watched a talk by Bill Kennedy and his precision of language really stood out to me.

As I considered writing about some of the fundementals of our trade, I started thinking about basic programming concepts like the composable parts of language. In linguistics, a morpheme is the smallest part of language that has inherent meaning. In English an example is the suffix “able”, meaning “capable of.” We all use morphemes all the time. We even know how to make up new works using these existing components, but we may not know how to precisely describe what we are doing. I knew how to write a closure, but it was difficult for me to explain it well.

Closures are a means of lexical scoping and abstraction in languages where functions are first class values. That sounds more complex than it really is. Let’s look an example.

Closure Example

Before I jump in I want to make it clear that we could totally do this using a struct that has a field instead. As with most problems, there are multiple solutions and each has it’s benefits and risks. This post isn’t really about the benefits of functional programming so I won’t go into that here, but there is some good information available.¹ ² In fact, closures are probably not the best way to write our client, but I will leave that as exercize for the reader to determine.

In our example, we have a client. A client that needs to be initialized with a url. We use our closure to create the client and “close” around it. Internally we are creating a client struct, but it’s hidden to the caller. This is kind of a weird example let’s look at a great example in Go.

In this example, we are creating a server. We have a closure in the function AccessLogger. This is a super common pattern in Go. We close around the logger and return a HandlerFunc. That way we can pass the server a standard handler and we can access our logger, or whater else you want to pass.

So essentially, a closure is a function that returns another function and provides that inner funtion access to some variable. That inner variable is immutable too so you don’t have to worry about it changing!

That’s about it. I like closures a lot. I think they are elegant and fun to use. I would love to hear what you think or if you have any suggestions for additional topics.

[1]: Bohdan B Khomtchouk, Edmund Weitz, Peter D Karp, and Claes Wahlestedt. (Dec 1 2016). How the strengths of Lisp-family languages facilitate building complex and flexible bioinformatics applications https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5952920/

[2]: Larry Hardestyarchive. (Oct 24 2017). Guaranteed Code https://www.technologyreview.com/2017/10/24/148412/guaranteed-code/

--

--

Guy J Grigsby
Star Gazers

Technologist, taco lover and Gopher. Technologist and software engineer in distributed systems. Neuro-divergent.