Building middleware functions is simple and straightforward. Let's build a program based on the knowledge gained from Chapter 2, Handling Routing for our REST Services. If you are not familiar with closure functions, a closure function returns another function. This principle helps us write middleware. A middleware should return another function, which can be either a middleware or a function handler. It is similar to JavaScript chain methods, whereby one function returns a new function as a return value. Let's create a closure function in Go, by doing the following:
- Create a program file, like so:
touch -p $GOPATH/src/github.com/git-user/chapter3/closureExample/main.go
We use this file to add our code.
- A closure ...