-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (55 loc) · 1.19 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
package main
import (
"fmt"
"net/http"
"strconv"
)
func main() {
http.HandleFunc("/calculate", calculateHandler)
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
return
}
}
func calculateHandler(w http.ResponseWriter, r *http.Request) {
// Parsing query parameters
query := r.URL.Query()
num1Str := query.Get("num1")
num2Str := query.Get("num2")
operation := query.Get("operation")
num1, err := strconv.Atoi(num1Str)
if err != nil {
http.Error(w, "Invalid num1", http.StatusBadRequest)
return
}
num2, err := strconv.Atoi(num2Str)
if err != nil {
http.Error(w, "Invalid num2", http.StatusBadRequest)
return
}
// Performing the operation
var result int
switch operation {
case "add":
result = num1 + num2
case "subtract":
result = num1 - num2
case "multiply":
result = num1 * num2
case "divide":
if num2 == 0 {
http.Error(w, "Cannot divide by zero", http.StatusBadRequest)
return
}
result = num1 / num2
default:
http.Error(w, "Unsupported operation", http.StatusBadRequest)
return
}
// Sending the response
_, err = fmt.Fprintf(w, "Result: %d", result)
if err != nil {
return
}
}