Golang HTTP server for pro

How to start a new web project with Go, using Routing, Middleware and Let’s Encrypt certification.

Golang have a great http server package: net/http As always, it’s simple and very powerful. Define the function that handle a route, and let’s listen to port 80.

package main



import (

"io"

"net/http"

) func main() {

http.HandleFunc("/", helloWorldHandler)

http.ListenAndServe(":80", nil)

} func helloWorldHandler(w http.ResponseWriter, r *http.Request) {

io.WriteString(w, "Hello world!")

}

Nice, but let’s use a more powerfull router like the Gorilla package: gorilla/mux http://www.gorillatoolkit.org/pkg/mux

It implements a request router and a dispatcher. It allows you to create routes with named parameters, restricted on http verb and host/domain management.

Gorilla Routing in action!

Updating the previous exemple with this package allow us to manage easily many routes with simples configurations: