How it works...
These strategies work because Go allows methods to satisfy typed functions such as http.HandlerFunc. By using a struct, we can inject various pieces in main.go, which could include database connections, logging, and more. In this recipe, we inserted a Storage interface. All handlers connected to the controller can make use of its methods and attributes.
The GetValue method doesn't have an http.HandlerFunc signature and instead returns it. This is how we can use a closure to inject state. In main.go, we define two routes one with UseDefault set to false and the other with it set to true. This could be used when defining a function that spans multiple routes, or when using a struct where your handlers feel too cumbersome.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access