kok202
[2019.03.08] Go lang (Html template)

2019. 3. 8. 18:29[공부] 영상/GoLanguage

htmlTemplate.html

<h1>{{ .Title }}</h1>
<h2>{{ .Description }}</h2>


Go

package main

import "net/http"
import "html/template"

type PageTemplate struct {
    Title string
    Description string
}

func indexHandler(writer http.ResponseWriter, reader *http.Request) {
    pageTemplate := PageTemplate{Title: "index", Description: "This is index page"}
    template, _ := template.ParseFiles("htmlTemplate.html")
    template.Execute(writer, pageTemplate)
}

func aboutHandler(writer http.ResponseWriter, reader *http.Request) {
    pageTemplate := PageTemplate{Title: "about", Description: "This is about page"}
    template, _ := template.ParseFiles("htmlTemplate.html")
    template.Execute(writer, pageTemplate)
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/index", indexHandler)
    http.HandleFunc("/about", aboutHandler)
    http.ListenAndServe(":8000", nil)
}