-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
37 lines (32 loc) · 840 Bytes
/
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
package main
import (
"html/template"
"log"
"net/http"
)
type Book struct {
Title string
Author string
}
func main() {
h1 := func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("views/index.html"))
books := map[string][]Book{
"Books": {
{Title: "Fun Times", Author: "Michael"},
{Title: "Deep Work", Author: "Cal"},
{Title: "ABC", Author: "Ken"},
},
}
tmpl.Execute(w, books)
}
h2 := func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("title")
author := r.PostFormValue("author")
tmpl := template.Must(template.ParseFiles("views/index.html"))
tmpl.ExecuteTemplate(w, "book-list-element", Book{Title: title, Author: author})
}
http.HandleFunc("/", h1)
http.HandleFunc("/add-book/", h2)
log.Fatal(http.ListenAndServe(":8000", nil))
}