[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)
}
'[공부] 영상 > GoLanguage' 카테고리의 다른 글
[2019.03.08] Go lang (Channel, Close) (0) | 2019.03.08 |
---|---|
[2019.03.08] Go lang (Go routine, sync) (0) | 2019.03.08 |
[2019.03.08] Go lang (Map, Defer, Panic, Recover) (0) | 2019.03.08 |
[2019.03.08] Go lang (Struct) (0) | 2019.03.08 |
[2019.03.08] Go lang (서버 제작) (0) | 2019.03.08 |