Adding middleware
Sometimes, you will need to create a lot of functions to handle HTTP requests, maybe serving different paths in a URL, all performing different actions. You might need to create a function to handle a server returning a list of users, one with a list of projects, a route for updating some details, and all the functions doing different things. It might happen, however, that although these functions perform different actions, they will also have something in common. A common example is when these functions have to be performed on a secured environment, which means only for users that have been logged in. Let’s look at a very simple example and consider the following two functions:
http.HandleFunc( "/hello1", func(w http.ResponseWriter, r *http.Request, ){ msg := "Hello there, this is function 1" w.Write([]byte(msg)) }) http.HandleFunc( "/hello2", ...