Running an HTTP server
The standard Go library offers an HTTP server with sensible defaults that you can use out of the box, similar to the way HTTP clients are implemented. If you need to configure transport specifics, timeouts, and so on, then you can create a new http.Server and work with it. This section describes both approaches.
How to do it...
- Create an
http.Handlerto handle HTTP requests:func myHandler(w http.ResponseWriter, req *http.Request) {   if req.Method == http.MethodGet {     // Handle an HTTP GET request   }   ... } - Call
http.ListenAndServe:err:=http.ListenAndServe(":8080",http.HandlerFunc(myHandler)) log.Fatal(err) - The
ListenAndServefunction either returns immediately due to an error setting up a network listener (for example, if the address is already in use) or successfully starts listening. When the server is asynchronously closed (by callingserver.Close()orserver.Shutdown()), it...