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 main
import
(
"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 : ", err)
return
}
}
func main()
{
router := mux.NewRouter()
router.HandleFunc("/", renderTemplate).Methods("GET")
router.PathPrefix("/").Handler(http.StripPrefix("/static",
http.FileServer(http.Dir("static/"))))
err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, router)
if err != nil
{
log.Fatal("error starting http server : ", err)
return
}
}
  1. Run the program with the following command:
$ go run serve-static-files-gorilla-mux.go
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87