Checkout https://www.gophersumit.com/series/web-server/ for more.
net/http package comes as part of standard library of Go. This provides HTTP client and server implementations. For building web servers with Go, there is no external library required.
package documentation https://golang.org/pkg/net/http/
simple web server
Writing a simple web server is trivial in Go.
- at line 3, we import
net/httppackage which provides required types functions to build web server.
- at line 10, we define a multiplexer which in responsible for routing incoming requests to various functions or handlers.It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
- at line 11, we define HandleFunc which is way of defining incoming requests mapping to functions. We are using match-all
/i.e. all the incoming requests will be handled by our single function.
- HandleFunc is higher order function which accepts another function as its second parameter.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
- We are able to pass
HandleFuncsince signature of
hellomatches to that of function which
- line 12 is responsible for starting web server at the specified port with specified multiplexer.
- line 6 is responsible for returning response of Hello, Gophers! to every incoming request.
Alternatively, server struct can be used to construct web server.
Server struct is defined in Go Std library as
As you can see, there are lots of configuration options that can be provided while spinning new web server.
For up to date definition with comments, refer to : https://golang.org/src/net/http/server.go?s=77156:81268#L2480
using server struct
we can rewrite web server defined above using server struct as
This is very similar to one we defined earlier.
Multiplexer is responsible for defining routing of web application. Most of the times it is desired to run different logic based on url user is trying to access. Multiplexer are use to accomplish this task.
Lets see how we can register different handlers for different routes.
routes.go defines the registrations for all the routes in our application while handler.go defines the handler functions to be used.
Handler in Go are responsible for handling incoming http request. In multiplexer route registration, we have defined handlers for various routes.
We can define handlers in Go using two ways:
struct type as handler
General convention is to use a struct as handler for incoming http request. For a struct to be a valid handler, it needs to satisfy Handler interface which is defined as
Let’s create our own http handler struct.
- Define struct type
- provide implementation for
CustomerHandler can be used anywhere we need type of
Let’s spin a simple web server using this custom handler.
function as handler
We can also use functions as handlers in Go. This is made possible by
HandlerFunc type defined in Go Std library.
HandlerFunctype is an adapter that accepts a function and returns type of
Handlerinterface by providing implementation for ServeHTTP.
HandlerFuncaccepts any function which has
*Requestas its parameters.
Let’s spin up simple server using function as handlers.
This is how handlers and multiplexers work in Go.