-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
428 lines (398 loc) · 10.6 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"awesomeProject/accessModifiers"
"awesomeProject/enum"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
"unicode"
)
type Employee struct {
FirstName string
LastName string
Age int
Degree string
}
// NewEmployeeWithAllArgs Employee all args constructor
func NewEmployeeWithAllArgs(firstName string, lastName string, age int, degree string) Employee {
return Employee{FirstName: firstName, LastName: lastName, Age: age, Degree: degree}
}
// NewEmployeeWithNoArgs Employee no args constructor
func NewEmployeeWithNoArgs() Employee {
return Employee{}
}
func main() {
// Hello world
fmt.Println("Hello world")
// public variable
fmt.Println(accessModifiers.Version)
// public getter func
fmt.Println(accessModifiers.GetName())
// Enum example
enum.PrintBrand(enum.BMW)
enum.PrintBrand(enum.MERCEDES)
// type convert
// string to int
myString1 := "123"
number, _ := strconv.Atoi(myString1)
fmt.Println(reflect.TypeOf(myString1), "to", reflect.TypeOf(number))
// string to bool
myString2 := "false"
myBoolean, _ := strconv.ParseBool(myString2)
fmt.Println(reflect.TypeOf(myString2), "to", reflect.TypeOf(myBoolean))
// type casting
myInt1 := 64
myFloat1 := float64(myInt1)
fmt.Println(reflect.TypeOf(myInt1), "to", reflect.TypeOf(myFloat1))
// formatter
// https://pkg.go.dev/fmt
myString3 := "alfa"
fmt.Printf("Data types : %T \n", myString3)
fmt.Printf("Value of myString3 variable : %v \n", myString3)
/*
// input operations from console
reader := bufio.NewReader(os.Stdin)
myString4, _ := reader.ReadString('\n')
fmt.Println("input data : ", myString4)
// input number value
myString5, _ := reader.ReadString('\n')
myFloat2, err := strconv.ParseFloat(strings.TrimSpace(myString5), 64)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Value of number : ", myFloat2)
}
*/
// Date and hour operations
specificDate := time.Date(2023, time.April, 3, 0, 40, 0, 0, time.UTC)
fmt.Printf("Specific date : %s\n", specificDate)
fmt.Println("The month is ", specificDate.Month())
fmt.Println("The day is ", specificDate.Day())
fmt.Println("The weekday is ", specificDate.Weekday())
nextYear := specificDate.AddDate(1, 0, 0)
fmt.Printf("The next year same time is : %s\n", nextYear)
longFormat := "Monday, January 2, 2006"
fmt.Println("Long format : ", nextYear.Format(longFormat))
shortformat := "2/1/2006"
fmt.Println("Short format :", nextYear.Format(shortformat))
fmt.Println("Timestamp : ", nextYear.Unix())
now := time.Now()
fmt.Printf("The time now is : %s\n", now)
// if condition block
a := 20
b := 10
if a < b {
fmt.Println("a smaller than b")
} else if a > b {
fmt.Println("a bigger than b")
} else {
fmt.Println("a equals b")
}
// variable definition at if scope
if x, y := 10, 20; x < y {
fmt.Println("Variables of x and y can only be called in the if scope.")
}
// switch condition
city := "Ankara"
switch city {
case "Ankara":
fmt.Println("This city is Ankara")
case "Istanbul":
fmt.Println("This city is Istanbul")
default:
fmt.Println("This city is not found")
}
grade := 75
switch {
case grade >= 0 && grade <= 59:
fmt.Println("Your alphanumeric grade is F")
case grade > 59 && grade <= 69:
fmt.Println("Your alphanumeric grade is D")
case grade > 69 && grade <= 79:
fmt.Println("Your alphanumeric grade is C")
case grade > 79 && grade <= 89:
fmt.Println("Your alphanumeric grade is B")
case grade > 89 && grade <= 100:
fmt.Println("Your alphanumeric grade is A")
default:
fmt.Println("Your grade is invalid")
}
// break, continue, goto keywords
// break
fmt.Print("i values :")
i := 0
for {
if i >= 3 {
break
}
fmt.Print(i, "\t")
i++
}
fmt.Print("\n")
// continue
fmt.Print("i values :")
for i := 0; i < 7; i++ {
if i%3 == 0 {
continue
}
if i == 5 {
break
}
fmt.Print(i, "\t")
}
fmt.Print("\n")
// goto
fmt.Print("i values :")
for i := 0; i < 7; i++ {
if i%3 == 0 {
continue
}
goto PRINT
if i == 5 {
break
}
PRINT:
fmt.Print(i, "\t")
}
fmt.Print("\n")
// loop
// for
fmt.Println("Loop \nfor")
fmt.Print("Values : ")
for i := 0; i <= 5; i++ {
fmt.Print(i, "\t")
}
fmt.Print("\n")
// while
fmt.Println("while")
fmt.Print("Values : ")
j := 1
for j < 10 {
fmt.Print(j, "\t")
j += j
}
fmt.Print("\n")
// range array example
fmt.Println("Print the exponents of 2 with 'range'.")
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
for i, v := range pow {
fmt.Printf("2**%d = %d\n", i, v)
}
// range map example
capitals := map[string]string{"Turkey": "Ankara", "France": "Paris", "Italy": "Rome"}
for country := range capitals {
fmt.Printf("The capital of %s is %s\n", country, capitals[country])
}
// range string example
myString6 := "golang"
for i := range myString6 {
fmt.Println(i, myString6[i])
}
// error handling
result, err := os.Open("test.txt")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
// create error object
myError := errors.New("This is my error")
fmt.Println(myError.Error())
// array
var colors = []string{"red", "green", "blue"}
fmt.Println(colors)
colors = append(colors, "yellow")
fmt.Println(colors)
colors = append(colors[1:len(colors)])
fmt.Println(colors)
// array with make
myArray := make([]int, 5, 5)
myArray[0] = 1
myArray[1] = 2
myArray[2] = 3
myArray[3] = 4
myArray[4] = 5
fmt.Println(myArray)
fmt.Println("append before len : ", len(myArray))
fmt.Println("append before cap : ", cap(myArray))
myArray = append(myArray, 6)
fmt.Println(myArray)
fmt.Println("append after len : ", len(myArray))
fmt.Println("append after cap : ", cap(myArray))
// array with initialization
myArray2 := [5]int{1, 2, 3, 4, 5}
fmt.Println(myArray2)
// array with initialization and ...
myArray3 := [...]int{1, 2, 3, 4, 5}
fmt.Println(myArray3)
// array with initialization and index
myArray4 := [...]int{1: 1, 3: 2, 5: 3}
fmt.Println(myArray4)
// slice
mySlice := myArray[1:4]
fmt.Println(mySlice)
mySlice2 := myArray[1:]
fmt.Println(mySlice2)
mySlice3 := myArray[:4]
fmt.Println(mySlice3)
// map
myMap := make(map[string]string)
myMap["name"] = "golang"
myMap["version"] = "1.14.2"
fmt.Println(myMap)
fmt.Println(myMap["name"])
fmt.Println(myMap["version"])
// map with initialization
myMap2 := map[string]string{"name": "golang", "version": "1.14.2"}
fmt.Println(myMap2)
// delete map
delete(myMap2, "name")
fmt.Println(myMap2)
// map with struct
type Person struct {
name string
surname string
age int
}
myMap3 := make(map[string]Person)
myMap3["person1"] = Person{"golang", "gopher", 1}
myMap3["person2"] = Person{"python", "snake", 2}
fmt.Println(myMap3)
fmt.Println(myMap3["person1"])
fmt.Println(myMap3["person2"])
// map traverse
for key, value := range myMap3 {
fmt.Println(key, value)
}
// map traverse with key
list := make([]string, 0, len(myMap3))
for key := range myMap3 {
list = append(list, key)
}
fmt.Println(list)
for _, v := range list {
fmt.Println(myMap3[v])
}
// strings package
fmt.Print("\nStrings package \n")
fmt.Println(strings.Contains("golang", "go"))
fmt.Println(strings.Count("golang", "o"))
fmt.Println(strings.HasPrefix("golang", "go"))
fmt.Println(strings.HasSuffix("golang", "lang"))
fmt.Println(strings.Index("golang", "l"))
fmt.Println(strings.Join([]string{"golang", "python", "java"}, "-"))
fmt.Println(strings.Repeat("golang\t", 3))
fmt.Println(strings.Replace("golang golang", "go", "python", 2))
fmt.Println(strings.Split("golang-python-java", "-"))
fmt.Println(strings.ToLower("GOLANG"))
fmt.Println(strings.ToUpper("golang"))
fmt.Println(strings.TrimSpace(" golang "))
fmt.Println(strings.Trim("golang", "g"))
fmt.Println(strings.TrimLeft("golang", "g"))
fmt.Println(strings.TrimRight("golang", "g"))
fmt.Println(strings.Fields("golang python java"))
fmt.Println(strings.FieldsFunc("golang,python,java", func(r rune) bool {
return r == ','
}))
fmt.Println(strings.SplitAfter("golang-python-java", "-"))
fmt.Println(strings.SplitAfterN("golang-python-java", "-", 2))
fmt.Println(strings.SplitN("golang-python-java", "-", 2))
fmt.Println(strings.Title("golang python java"))
fmt.Println(strings.ToTitle("golang python java"))
fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
fmt.Println(strings.TrimPrefix("golang", "go"))
fmt.Println(strings.TrimSuffix("golang", "lang"))
fmt.Println(strings.Compare("golang", "golang"))
fmt.Println(strings.EqualFold("golang", "GOLANG"))
fmt.Println(strings.NewReplacer("golang", "python", "python", "java").Replace("golang python"))
fmt.Println(strings.ReplaceAll("golang python", "golang", "python"))
// function
fmt.Print("\nFunction \n")
fmt.Println(add(1, 2, "sum"))
fmt.Println(add(1, 2, "sub"))
fmt.Println(add(1, 2, "mul"))
fmt.Println(add(1, 2, "div"))
fmt.Println(add(1, 2, "other"))
// named return
fmt.Println(namedReturn(1, 2, "sum"))
// function multiple return
myString7, myString8 := "alfa", "beta"
myString7, myString8 = swap(myString7, myString8)
fmt.Println(myString7, myString8)
// function with multiple variable
fmt.Println("Total : ", totalVariable(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
// function with pointer
var message string = "golang"
pointerFunc(&message)
fmt.Println(message)
// function with defer
deferFunc()
// struct
employee := Employee{FirstName: "golang", LastName: "gopher", Age: 1, Degree: "bachelor"}
fmt.Println(employee)
// create struct with new
employee2 := new(Employee)
employee2.FirstName = "python"
employee2.LastName = "snake"
employee2.Age = 2
employee2.Degree = "master"
fmt.Println(*employee2)
// create struct with all args constructor
employee3 := NewEmployeeWithAllArgs("java", "coffee", 3, "doctor")
fmt.Println(employee3)
// create struct with no args constructor
employee4 := NewEmployeeWithNoArgs()
fmt.Println(employee4)
}
// get pointer of variable
func pointerFunc(message *string) {
fmt.Println(*message)
*message = "python"
}
// function
func add(x, y int, z string) int {
if z == "sum" {
return x + y
} else if z == "sub" {
return x - y
} else if z == "mul" {
return x * y
} else if z == "div" {
return x / y
} else {
return 0
}
}
func namedReturn(x, y int, z string) (result int) {
if z == "sum" {
result = x + y
} else if z == "sub" {
result = x - y
} else if z == "mul" {
result = x * y
} else if z == "div" {
result = x / y
} else {
result = 0
}
return
}
func swap(x, y string) (string, string) {
return y, x
}
func totalVariable(variables ...int) int {
total := 0
for _, v := range variables {
total += v
}
return total
}
func deferFunc() {
fmt.Println("first line")
defer fmt.Println("middle line (defer)")
fmt.Println("last line")
}