-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler_get.go
57 lines (47 loc) · 1.22 KB
/
request_handler_get.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"fmt"
"log"
)
type resp_format struct {
Result int `json:"result"`
Success bool `json:"success"`
}
func main() {
http.HandleFunc("/add", add_params)
http.HandleFunc("/sub", sub_params)
http.ListenAndServe(":9988", nil)
}
func add_params(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
a, _ := strconv.Atoi(params.Get("a"))
b, _ := strconv.Atoi(params.Get("b"))
sum_val := a + b
val := resp_format{Result: sum_val, Success: true}
resp, err := json.Marshal(val)
if err != nil {
fmt.Println("error =", err)
return
}
log.Println(r)
w.Header().Set("Content-Type", "application/json; char-set: utf-8")
w.Write(resp)
}
func sub_params(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
a, _ := strconv.Atoi(params.Get("a"))
b, _ := strconv.Atoi(params.Get("b"))
sub_val := a - b
val := resp_format{Result: sub_val, Success: true}
resp, err := json.Marshal(val)
if err != nil {
fmt.Println("error =", err)
return
}
log.Println(r)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}