-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (135 loc) · 3.11 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"errors"
"fmt"
"math"
"net/http"
"os"
"github.com/RK-GCP/my-go-service/controllers"
"github.com/RK-GCP/my-go-service/models"
)
func main() {
controllers.RegisterControllers()
http.ListenAndServe(":3000", nil)
}
//TryPanic - example
func TryPanic() {
_, err := startwebserver(8200)
if err != nil {
_, errFile := os.Create("/tmp/file.txt")
panic(errFile)
}
}
//TryingLoops -Example
func TryingLoops() {
//simple for loop
s := []int{11, 21, 39}
for i := 0; i < len(s); i++ {
fmt.Println("Simple Loop: ", s[i])
}
//go way using indexer and value - works for slice, array, and map
for i, v := range s {
fmt.Println("Index Value Loop: ", i, v)
}
tp := map[string]int{"http": 80, "https": 443}
//Value Only
for _, val := range tp {
fmt.Println("Value only: ", val)
}
//Keys only
for k := range tp {
fmt.Println("key only: ", k)
}
//both Key and Value
for k, v := range tp {
fmt.Println(k, ":", v)
}
}
func manything2() {
fmt.Println("Hello World")
ec, err := startwebserver(3000)
fmt.Println(ec, err)
//if only need one piece of data from func data return use underscore for data not interested
_, err2 := startwebserver(9000)
fmt.Println("Single Value: ", err2)
manything()
}
func startwebserver(port int) (int, error) {
fmt.Println("Starting web server...")
//code to start a web server.handler
fmt.Println("web server started at port: ", port)
er := errors.New("Not implemented")
errorcode := 501
return errorcode, er
}
func manything() {
fmt.Println("******* MANY THINGS *********")
tRate := 4.75
fmt.Println("Current rate", tRate)
//pointer examples
var testString *string = new(string)
*testString = "my current pointer"
fmt.Println(*testString, testString)
*testString = "hey new string"
fmt.Println(*testString, testString)
something := "I am not pointer"
ptr := &something
fmt.Println(*ptr, ptr)
something = "I am a pointer now"
fmt.Println(*ptr, ptr)
//function call
fmt.Println(split(18))
fmt.Println(getPi())
//array
t := [3]int{1, 4, 78}
fmt.Println("Array ia:", t)
//slice
mySlice := t[:]
mySlice[2] = 88
fmt.Println("A Slice from Array: ", mySlice)
mSlice2 := []string{"one", "two", "three"}
mSlice2 = append(mSlice2, "four", "five")
fmt.Println("Self Init Slice: ", mSlice2)
s1 := mSlice2[0:3]
s2 := mSlice2[:2]
s3 := mSlice2[1:2]
fmt.Println(s1, s2, s3)
//map
m1 := map[string]int{"foo": 40, "bar": 38}
fmt.Println(m1)
fmt.Println("Foo is: ", m1["foo"])
delete(m1, "bar")
fmt.Println("After deleting bar: ", m1)
//custom type
u := models.User{
ID: 2,
FirstName: "Ping",
LastName: "Pong",
}
fmt.Println(u)
}
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func getPi() float32 {
return math.Pi
}
//HTTPRequest type
type HTTPRequest struct {
Method string
}
//TryASwitch - Example
func TryASwitch() {
r := HTTPRequest{Method: "GET"}
switch r.Method {
case "GET":
fmt.Println("We got GET")
//fallthrough //continues to the next statement - not sure what is use case
case "PUT":
fmt.Println("we got PUT")
default:
fmt.Println("we have somthing using default")
}
}