Go ecoserver
This post is about a simple Go program that acts as web server which sends back a response with details of the request.
First we look at the elements of the program that we need to understand before we look at the complete program.
To run a server on port 8000 call the function ListenAndServe()
in the http
package. It takes the host:8000
address string as the first parameter and a handler function as the second parameter which could be nil.
This method once called will be continuously serving on that port until there is a fatal error or it is stopped.
To log an error and exit with status 1, use Fatal(msg)
method in log
package.
Any request to the above server are dispatched to one of the calls to the function HandleFunc(pattern string, handler func(ResponseWriter, *Request))
.
Which of these is called depends on the url of the incoming request.
The pattern ‘/’ will catch all requests coming to this server.
The function will have access to the whole request and will have to generate the response that will be sent to the server which the server will display as a result of the HTTP call.
The most important properties of the request such as the method (GET), URL (/abc), and protocol (HTTP/1.1) are accessed by the fields Method
, URL
and Proto
on the request pointer passed into the handler function.
The headers are accessible as the map Header
on the request. We can iterate over it to get all the headers.
To write to the response writer, we can use the function Fprintf(w io.Writer, format string, a ...interface{})
in the fmt
package.
If we need to check the success of a call and have no other need from a call, we can do the call, assign the result to a variable, and then check the error, all in one line such as ` if err := r.ParseForm(); err!= nil { log.Print(err)}`