-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
93 lines (84 loc) · 2.53 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
)
// Sub type holds user submissions
type Sub struct {
Username string
Num int
MyFloat float64
Updates bool
}
var tpl *template.Template
func main() {
tpl, _ = tpl.ParseGlob("templates/*.html")
http.HandleFunc("/getform", getFormHandler)
http.HandleFunc("/processget", processGetHandler)
http.HandleFunc("/postform", postFormHandler)
http.HandleFunc("/processpost", processPostHandler)
http.ListenAndServe(":8080", nil)
}
func getFormHandler(w http.ResponseWriter, r *http.Request) {
tpl.ExecuteTemplate(w, "getform.html", nil)
}
func processGetHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("processGetHandler running")
var s Sub
s.Username = r.FormValue("username")
// cannot use r.FormValue("numberName") (type string) as type int in assignment
// s.Num = r.FormValue("numberName")
num := r.FormValue("numberName")
// ASCII to int
// func Atoi(s string) (int, error)
s.Num, _ = strconv.Atoi(num)
s.Num = s.Num * 2
var err error
/*
// invalid syntax cannot parse float64
s.Num, err = strconv.Atoi(r.FormValue("myFltName"))
fmt.Println("error:", err)
*/
// func ParseFloat(s string, bitSize int) (float64, error)
s.MyFloat, err = strconv.ParseFloat(r.FormValue("myFltName"), 64)
if err != nil {
log.Fatal("error parsing float64")
}
if r.FormValue("upName") == "true" {
s.Updates = true
} else if r.FormValue("upName") == "false" {
s.Updates = false
}
tpl.ExecuteTemplate(w, "thanks.html", s)
}
func postFormHandler(w http.ResponseWriter, r *http.Request) {
tpl.ExecuteTemplate(w, "postform.html", nil)
}
func processPostHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("processPostHandler running")
var s Sub
s.Username = r.FormValue("username")
// cannot use r.FormValue("numberName") (type string) as type int in assignment
// s.Num = r.FormValue("numberName")
num := r.FormValue("numberName")
// ASCII to int
// func Atoi(s string) (int, error)
s.Num, _ = strconv.Atoi(num)
s.Num = s.Num * 2
var err error
// func ParseFloat(s string, bitSize int) (float64, error)
s.Num, _ = strconv.Atoi(r.FormValue("myFltName"))
//s.MyFloat, err = strconv.ParseFloat(r.FormValue("myFltName"), 64)
if err != nil {
log.Fatal("error parsing float64")
}
if r.FormValue("upName") == "true" {
s.Updates = true
} else if r.FormValue("upName") == "false" {
s.Updates = false
}
tpl.ExecuteTemplate(w, "thanks.html", s)
}