书写技术成长之路

GO渲染HTML

server.go

package main

import (
    "net/http"
    "log"
    "html/template"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
    t, _ := template.ParseFiles("templates/hello.html")
    t.Execute(w, "this is template data")
}

func main() {
    http.HandleFunc("/hello", sayHello)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

hello.html

<h1>Hello, Gopher!</h1>

<h3>{{.}}</h3>

参考

https://www.jianshu.com/p/05671bab2357