-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (85 loc) · 1.81 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"errors"
"net/http"
)
/*
Run All -> go test -bench=.
Run test only -> go test -run Test*
Run benchmark only -> go test -run Benchmark* -bench=.
Run benchmark only parallel -> go test -run Benchmark* -bench=. -cpu 4
Run examples only -> go test -run Example*
*/
func main() {
println("Math Methods: ")
a := 4
b := 2
println("A: ", a)
println("B: ", b)
result, err := add(a, b)
if err != nil {
println(err)
}
println("Add: ", result)
result, err = subtract(a, b)
if err != nil {
println(err)
}
println("Subtract: ", result)
result, err = multiply(a, b)
if err != nil {
println(err)
}
println("Multiply: ", result)
result, err = divide(a, b)
if err != nil {
println(err)
}
println("Divide: ", result)
go createWebserver()
}
func add(a int, b int) (int, error) {
if a > 10 || b > 10 {
return 0, errors.New("Numbers too big")
}
return a + b, nil
}
func subtract(a int, b int) (int, error) {
if a > 10 || b > 10 {
return 0, errors.New("Numbers too big")
}
return a - b, nil
}
func multiply(a int, b int) (int, error) {
if a > 10 || b > 10 {
return 0, errors.New("Numbers too big")
}
return a * b, nil
}
func divide(a int, b int) (int, error) {
if a > 10 || b > 10 {
return 0, errors.New("Numbers too big")
}
if b == 0 {
return 0, errors.New("You can not divide by zero")
}
return a / b, nil
}
func createWebserver() {
http.HandleFunc("/", processCall)
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
println("Error!!", err)
}
}
func processCall(w http.ResponseWriter, r *http.Request) {
headerMap := w.Header()
headerMap.Add("Go-Test", "Test 1.0")
switch r.Method {
case "GET":
w.Write([]byte("Test webserver"))
case "POST":
headerMap.Add("Unauthorized", "You need to have a valid token")
w.WriteHeader(401)
}
}