Tuesday 6 October 2020

go web app 2 templates

 
//tutorial.go
package main

import (
"net/http"
"html/template"
"github.com/gorilla/mux"
)

var templates *template.Template

func indexHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", nil)
}

func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}

//templates/index.html
<html>

<head>
    <title> awesome amazing page</title>
</head>

<body>
    <h1>Some basic HTML code</h1>
</body>

</html>

reference:

No comments:

Post a Comment