-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawesome.go
187 lines (165 loc) · 4.17 KB
/
awesome.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
//Every program start with package named with main
package main
import (
"awesome-go/arrayExample"
"awesome-go/channelExample"
"awesome-go/errorExample"
"awesome-go/functionExample"
"awesome-go/goRoutineExample"
"awesome-go/interfaceExample"
"awesome-go/mapExample"
"awesome-go/methodExample"
"awesome-go/pointerExample"
"awesome-go/sliceExample"
"awesome-go/structExample"
"fmt"
"math"
"runtime"
"time"
)
var c, python, java bool
const Pi = 3.14 //Const's variables can not be declared using with ":="
//An untyped numeric constant takes the type needed by its context.
func main() {
var i int
fmt.Println(i)
fmt.Println("Hello world.")
fmt.Println(add(2, 3))
fmt.Println(addWithEasyArg(2, 3))
a, b := 2, 3
fmt.Printf("Before swap a:%v b:%v\n", a, b)
a, b = swap(a, b)
fmt.Printf("After swap a:%v b:%v\n", a, b)
fmt.Println(swapTimes2NakedReturn(a, b))
fmt.Println(python)
fmt.Println(c, python, java)
var csharp, pythons, java = true, false, "no!"
fmt.Println(csharp, pythons, java)
//Unlike in C, in Go assignment between items of different type requires an explicit conversion.
var x, y int = 3, 4
var f float64 = math.Sqrt(float64(x*x + y*y))
var z uint = uint(f)
//Or
//i := 42
//f := float64(i)
//z := uint(f)
fmt.Println(x, y, z)
forExample()
fmt.Println(ifExamplePow(3, 2, 10))
switchExample()
deferExample()
deferExample2()
pointerExample.Pointers()
structExample.StructExample()
arrayExample.ArrayExample()
sliceExample.SliceExample()
mapExample.MapExample()
functionExample.FunctionExample()
methodExample.MethodExample()
interfaceExample.InterfaceExample()
errorExample.ErrorExample()
goRoutineExample.GoRoutineExample()
channelExample.ChannelExample()
}
//Basic func
func add(x int, y int) int {
return x + y
}
//If you use more than one same type arg. you don't have to declare type but last one
func addWithEasyArg(x, y int) int {
return x + y
}
// Func can return more than one result
func swap(a, b int) (int, int) {
return b, a
}
//In go you can name return values
//Naked returns is much better for short func
func swapTimes2NakedReturn(a, b int) (x, y int) {
x = b * 2
y = a * 2
return
}
func forExample() {
sum := 0
//There is no need parentheses like in other programming languages
for i := 0; i < 10; i++ {
sum += i
//There is no need parentheses for if condition
if sum > 10 {
fmt.Printf("Sum is higher than 10. Sum: %v\n", sum)
}
}
fmt.Println(sum)
//init and post statement are optional. So it is basically "while" statement
sum2 := 1
for sum2 < 1000 {
sum2 += sum2
}
fmt.Println(sum2)
//infinite loop
/*
for {
}
*/
}
func ifExamplePow(x, n, lim float64) float64 {
//Like for statement you can put nit statement for if in go
if v := math.Pow(x, n); v < lim {
return v
}
return lim
}
func switchExample() {
//n effect, the break statement that is needed at the end of each case
//in other languages is provided automatically in Go.
//Because of that there is no need break everywhere
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.\n", os)
}
//Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
fmt.Println("When's Saturday?")
today := time.Now().Weekday()
switch time.Saturday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
//If there is no condition it is means that "switch true"
//This construct can be a clean way to write long if-then-else chains.
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
func deferExample() {
defer fmt.Println("after surrounding func return")
fmt.Println("You will see")
fmt.Println("defer result ")
}
func deferExample2() {
fmt.Println("counting")
//When a function returns, its deferred calls are executed in last-in-first-out order like a stack
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
fmt.Println("done")
}