In Java, it is pretty common to see libraries that use method chaining (aka the builder pattern) to construct resources. For example, we might construct a user by doing something like: Useruser=newUser.Builder() .name("Michael Scott") .email("firstname.lastname@example.org") .role("manager") .nickname("Best Boss") .build(); Builders are handy for a variety of reasons, but in the example above we are using a builder in order to define a subset of our User attributes before constructing the user object.
Storing data in a context.Context, or as I refer to it - using context values, is one of the most contentious design patterns in Go. Storing values in a context appears to be fine with everyone, but what specifically should be stored as a context value receives a lot of heated discussion. I’ll be honest - when I started using them I used them in the naive and somewhat inappropriate way that everyone complains about.
This article explores a couple easy to make but hard to debug mistakes that can be made when working with closures in Go, and explains how to avoid them.
Explore five different real world use cases for closures that will help you understand and utilize closures better in your own code.
An explanation of what anonymous functions and closures are, along with examples of how to create and use them in Go (aka Golang).
Jon Calhoun is a full stack web developer who also teaches about Go, web development, algorithms, and anything programming related. He also consults for other companies who have development needs. (If you need some development work done, get in touch!)
Jon is a co-founder of EasyPost, a shipping API that many fortune 500 companies use to power their shipping infrastructure, and prior to founding EasyPost he worked at google as a software engineer.
Jon's latest progress update: Writing Course Notes
©2018 Jonathan Calhoun. All rights reserved.