-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
61 lines (51 loc) · 1.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
)
const (
port = 8000
)
type Address struct {
StreetAddress string
City string
State string
ZipCode string
}
type Wrap struct {
Data map[string]interface{}
}
func main() {
files, err := filepath.Glob("templates/*.gohtml")
if err != nil {
log.Panic(err)
}
// Parse and load the templates.
tmpl, err := template.ParseFiles(files...)
if err != nil {
log.Panic(err)
}
// Wrap the multiple values into a parameter using map.
data := map[string]interface{}{
"Title": "Your Order",
"Address": Address{
StreetAddress: "1 Main Street",
City: "Springfield",
State: "CA",
ZipCode: "90405",
},
"TotalCost": 50.0,
}
wrap := Wrap{Data: data}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Render the template.
if err := tmpl.ExecuteTemplate(w, "home", wrap); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
log.Println("web server running at port", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}