struct
//tutorial.go
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
type car struct {
Color string
Mileage int
Model string
}
var templates *template.Template
var truck = car{
Color: "black",
Mileage: 12345,
Model: "F-150",
}
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", truck)
}
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexGetHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
----------------------------
//index.html
<html>
<head>
<title> Templates</title>
</head>
<body>
<h1>Ford {{.Model}}</h1>
<p>Mileage: {{.Mileage}}</p>
<p>Color: {{.Color}}</p>
</body>
</html>
array
//tutorial.gopackage main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
var templates *template.Template
var colors = []string{"red", "green", "blue"}
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", colors)
}
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexGetHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
-------------------------
//index.html
<html>
<head>
<title> Templates</title>
</head>
<body>
<h1>Colors</h1>
<ul>
{{range $index, $item := .}}
<li>{{$index}} - {{$item}}</li>
{{end}}
</ul>
</body>
</html>
map
//tutorial.go
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
var templates *template.Template
var grades = map[string]int{
"homework": 100,
"quize": 100,
"midterm": 100,
"final": 100,
}
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", grades)
}
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexGetHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
-------------------------
//index.html
<html>
<head>
<title> Templates</title>
</head>
<body>
<h1>Grades</h1>
<ul>
{{range $key, $val := .}}
<li>{{$key}} - {{$val}}</li>
{{end}}
</ul>
</body>
</html>
struct array
//tutorial.go
package main
import (
"html/template"
"net/http"
"github.com/gorilla/mux"
)
type car struct {
Color string
Mileage int
Model string
}
var templates *template.Template
var truck = car{
Color: "black",
Mileage: 12345,
Model: "F-150",
}
var sedan = car{
Color: "white",
Mileage: 321,
Model: "Corolla",
}
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", []car{truck, sedan})
}
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexGetHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
----------------------------
//index.html
<html>
<head>
<title> Templates</title>
</head>
<body>
<h1>Cars</h1>
<ul>
{{range .}}
<li>{{.Color}} {{.Model}} - {{.Mileage}} Miles</li>
{{end}}
</ul>
</body>
</html>
reference:
No comments:
Post a Comment