-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (42 loc) · 879 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
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"net/http"
"strconv"
)
func addHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
aStr := r.FormValue("a")
bStr := r.FormValue("b")
a, _ := strconv.Atoi(aStr)
b, _ := strconv.Atoi(bStr)
c := a + b
result := fmt.Sprintf("Result: %d", c)
html := `
<!DOCTYPE html>
<html>
<head>
<title>Addition Result</title>
</head>
<body>
<h1>Addition Result</h1>
<p>%s</p>
<form method="post">
<label for="a">Enter a:</label>
<input type="text" name="a"><br>
<label for="b">Enter b:</label>
<input type="text" name="b"><br>
<button type="submit">Calculate</button>
</form>
</body>
</html>
`
fmt.Fprintf(w, html, result)
} else {
http.ServeFile(w, r, "index.html")
}
}
func main() {
http.HandleFunc("/", addHandler)
http.ListenAndServe(":8080", nil)
}