Creating a basic middleware

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:

  1. 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.

  1. A closure ...

Get Hands-On RESTful Web Services with Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.