O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it…

  1. Install the github.com/gorilla/mux package using the go get command, as follows:
$ go get github.com/gorilla/mux
  1. Create serve-static-files-gorilla-mux.go, where we will create a Gorilla Mux router instead of an HTTP FileServer, as follows:
package mainimport (  "html/template"  "log"  "net/http"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")type Person struct {  Id string  Name string}func renderTemplate(w http.ResponseWriter, r *http.Request) {  person := Person{Id: "1", Name: "Foo"}  parsedTemplate, _ := template.ParseFiles("templates/  first-template.html")  err := parsedTemplate.Execute(w, person)  if err != nil   {    log.Printf("Error occurred while executing the template  or writing its output : ", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required