December 2017
Intermediate to advanced
316 pages
6h 58m
English
Building middleware is simple and straightforward. Let us build a program based on the knowledge gained from the second chapter. If you are not familiar with closure functions, a closure function returns another function. This principle helps us write middleware. The first thing we should do is implement a function that satisfies the http.Handler interface.
A sample closure called closure.go looks like this:
package mainimport ( "fmt")func main() { numGenerator := generator() for i := 0; i < 5; i++ { fmt.Print(numGenerator(), "\t") }}// This function returns another functionfunc generator() func() int { var i = 0 return func() int { i++ return i }}If we run this code:
go run closure.go
Numbers will be generated ...
Read now
Unlock full access