-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (190 loc) · 4.35 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"goTutorial3/mapTest"
"goTutorial3/structTest"
)
type person struct {
name string
age int
country string
}
// VowelFinder interface
type VowelFinder interface {
FindVowels() []rune
}
type myInt int
type myString string
func main() {
//variadic
findNum(12, 8, 3, 54, 90, 10, 12)
//zero args for variadic parameters
findNum(3)
//passing a slice with ellipsis
nums := []int{12, 43, 78, 90, 11}
findNum(43, nums...)
welcome := []string{"hello", "world"}
change(welcome...)
fmt.Println(welcome)
fmt.Println()
//map
ids := mapTest.StudentIds
mapTest.AddItems(ids)
mapTest.AddItems2()
//nil map
var map101 = mapTest.NilMap
fmt.Println("Nil map: ", map101)
//retrieve values for a given key
id := 234
student := ids[id]
fmt.Println("Student with the id ", id, " is ", student)
fmt.Println("Student with the id ", 7009, " is ", ids[7009])
mapTest.FindItem(2)
//iterate
for key, value := range ids {
fmt.Printf("Key: %d, value: %s \n", key, value)
}
//delete
fmt.Println("Before deleting: ", ids)
delete(ids, 89)
fmt.Println("After deleting: ", ids)
//length
fmt.Println("Length is ", len(ids))
var word = "Hello"
//print bytes - utf-8
for i := 0; i < len(word); i++ {
fmt.Printf("String %x \n", word[i])
}
//print characters
for i := 0; i < len(word); i++ {
fmt.Printf("Character %c \n", word[i])
}
printChars("Señor")
fmt.Println()
//pointers
var pointer *string = &word
fmt.Println(pointer)
size := new(int)
fmt.Println(size)
// de referencing a pointer
*size = 85
fmt.Println(size, *size)
fmt.Println()
//struct
structTest.CreateEmployee()
//anonymous struct
emp := structTest.Emp
fmt.Println(emp)
//nested struct
structTest.NestedStruct()
fmt.Println()
//methods
person1 := person{
name: "Ben",
age: 40,
country: "Sweden",
}
fmt.Println("Original age: ", person1.age)
//value receiver method
newAge := person1.increaseAge(2)
fmt.Println("After increase method ", person1.age)
fmt.Println("New age ", newAge)
fmt.Println()
//pointer receiver method
newAge2 := person1.decreaseAge(5)
fmt.Println("After decrease method ", person1.age)
fmt.Println("New age ", newAge2)
fmt.Println()
//normal function
newName := changeName(person1, "Mike")
fmt.Println("After change name function ", person1.name)
fmt.Println("newName ", newName)
var num myInt = 12
fmt.Println("Add num ", num.addNum(myInt(3)))
//interfaces
name := myString("Shenali Jayakody")
var v VowelFinder
v = name
fmt.Printf("Vowels %c", v.FindVowels())
//concrete type
fmt.Printf("Interface type is %T value is %v \n", v, v)
v, ok := v.(myString)
fmt.Println(v, ok)
fmt.Println()
findType("Hello")
findType(90)
findType(12.2)
findType(name)
}
func findNum(findMe int, list...int) {
found := false
for i,v := range list {
if v == findMe {
fmt.Println("Found ", findMe, " in ", i, "th index")
found = true
}
}
if !found {
fmt.Println("Not found")
}
}
func change(s ...string) {
s[0] = "Go"
s = append(s, "playground")
fmt.Println("S: ", s)
}
func printChars(s string) {
for i := 0; i < len(s); i++ {
fmt.Printf("Chars %c \n", s[i])
}
fmt.Println()
runes := []rune(s)
for i := 0; i < len(runes); i++ {
fmt.Printf("Rune %c \n", runes[i])
}
}
//value receiver
//change inside the method is not visible to the caller
func (p person) increaseAge(increaseBy int) int {
p.age += increaseBy
fmt.Println("Inside increase method ", p.age)
return p.age
}
//pointer receiver
//change inside is visible to the caller
func (p *person) decreaseAge(decreaseBy int) int {
p.age -= decreaseBy
fmt.Println("Inside decrease method ", p.age)
return p.age
}
//normal function
func changeName(p person, newName string) string {
p.name = newName
return p.name
}
//method with no-struct type receiver
func (a myInt) addNum(b myInt) myInt {
return a + b
}
// FindVowels myString implements VowelFinder interface
func (s myString) FindVowels() []rune {
var vowels []rune
for _, rune1 := range s {
if rune1 == 'a' || rune1 == 'e' || rune1 == 'i' || rune1 == 'o' || rune1 == 'u' {
vowels = append(vowels, rune1)
}
}
return vowels
}
func findType(i interface{}) {
switch i.(type) {
case string:
fmt.Println("String - ", i.(string))
case int:
fmt.Println("Int - ", i.(int))
case VowelFinder:
fmt.Println("Implements VowelFinder")
default:
fmt.Println("Unknown Type")
}
}