-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
215 lines (198 loc) · 5.63 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
// Package main is the entry point of the Go program.
package main
import (
"fmt"
"time"
)
// The main function is the entry point of the program.
func main() {
// BUILT-IN TYPES
// Declare and initialize variables using the var keyword.
var ninja = "Johnny"
var level, yoe int = 1, 2
var isSkilled bool
weapon := "Ninja Star"
fmt.Println(ninja, level, yoe, isSkilled, weapon)
// CONSTANTS
// Declare constants using the const keyword.
const dojo string = "Golang Dojo"
const powerLevel = 9001
const opLevel = 3e20
fmt.Printf("%T\n", opLevel)
// LOOPS
// Use for loops to iterate over a range of values.
isSkilled = true
for isSkilled {
fmt.Println("Ready for mission!")
isSkilled = false
}
for level := 7; level < 9; level++ {
fmt.Println(level)
fmt.Println("Leveling up!")
}
for {
fmt.Println("I'm a Golang Ninja")
break
}
// SWITCH
// Use switch statements to handle different cases.
weapon = "Ninja Star"
switch weapon {
case "Ninja Star":
fmt.Println("It's a Ninja Star!")
case "Ninja Sword":
fmt.Println("It's a Ninja Sword!")
}
powerLevel := 9001
switch {
case powerLevel > 9000:
fmt.Println("It's over...NINE THOUSAND!!!")
default:
fmt.Println("It's a Baby Ninja")
}
// ARRAYS
// Declare and initialize arrays using the var keyword.
var evilNinjas [3]string
fmt.Println(len(evilNinjas))
evilNinjas[0] = "Johnny"
fmt.Println(evilNinjas)
fmt.Println(evilNinjas[0])
fmt.Println(len(evilNinjas))
moreEvilNinjas := [3]string{"Andy", "Tommy", "Bobby"}
fmt.Println(moreEvilNinjas)
var missionRewards [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
missionRewards[i][j] = i + j
}
}
// SLICES
// Declare and initialize slices using the var keyword.
var evilNinjasSlice []string
fmt.Println(len(evilNinjasSlice))
evilNinjasSlice = append(evilNinjasSlice, "Tommy")
fmt.Println(len(evilNinjasSlice))
// MAPS
// Declare and initialize maps using the make function.
ninjaLevels := make(map[string]int)
ninjaLevels["Johnny"] = 7
ninjaLevels["Tommy"] = 13
fmt.Println(ninjaLevels)
fmt.Println(len(ninjaLevels))
delete(ninjaLevels, "Johnny")
fmt.Println(len(ninjaLevels))
_, ok := ninjaLevels["Tommy"]
fmt.Println(ok)
moreNinjaLevels := map[string]int{"Bobby": 8, "Andy": 3}
fmt.Println(moreNinjaLevels)
// RANGE
// Use range to iterate over slices and maps.
evilNinjasRange := []string{"Tommy", "Johnny", "Andy"}
for index, evilNinja := range evilNinjasRange {
fmt.Println("Attacking target", index, evilNinja)
}
evilNinjasWithLevels := map[string]int{"Tommy": 2}
for evilNinja, level := range evilNinjasWithLevels {
fmt.Printf("%s -> %d\n", evilNinja, level)
}
// STRUCTS
// Declare and initialize structs using the type keyword.
type ninja struct {
name string
level int
}
fmt.Println(ninja{name: "Bobby", level: 20})
fmt.Println(ninja{name: "Andy", level: 30})
fmt.Println(ninja{name: "Johnny"})
tommy := ninja{name: "Tommy", level: 50}
fmt.Println(tommy.level)
tommy.level = 51
// POINTERS
// Declare and initialize pointers using the & operator.
tommyPointer := &tommy
johnnyPointer := &ninja{"Johnny", 0}
var ninjaPointer *ninja = new(ninja)
fmt.Println(tommyPointer, johnnyPointer, ninjaPointer)
// INTERFACE
// Declare and initialize interfaces using the type keyword.
type Weapon interface {
attack()
}
type ninjaStar struct{}
type ninjaSword struct{}
// Method for ninjaStar
var _ = func(n ninjaStar) {
fmt.Println("Throwing Ninja Star")
}
}
// Method for ninjaSword
func (n ninjaSword) attack() {
fmt.Println("Throwing Ninja Sword")
}
// Create a slice of Weapon interface
weapons := []Weapon{
ninjaStar{},
ninjaSword{},
}
// Iterate over weapons and call attack method
for _, weapon := range weapons {
weapon.attack()
}
// FUNCTIONS
// Declare and initialize functions using the func keyword.
// Anonymous function to use a weapon
useWeapon := func(ninja string, weapon string) string {
return fmt.Sprintf(ninja + " is using " + weapon)
}
// Anonymous function to validate level
isValidLevel := func(level int) (int, bool) {
if level > 10 {
return level, true
}
return level, false
}
// Variadic function to attack multiple targets
attack := func(evilNinjas ...string) {
for _, evilNinja := range evilNinjas {
fmt.Println("Attacking target", evilNinja)
}
}
usage := useWeapon("Tommy", "Ninja Star")
level, valid := isValidLevel(11)
fmt.Println(usage, level, valid)
attack("Tommy", "Johnny")
attack("Tommy", "Johnny", "Andy", "Bobby")
evilNinjasAttack := []string{"Tommy", "Johnny", "Andy"}
attack(evilNinjasAttack...)
attackToo := attack
attackToo(evilNinjasAttack...)
// Immediately invoked function expression (IIFE)
func() {
fmt.Println("Attacking Evil Ninjas...")
}()
// GOROUTINES
// Use goroutines to run functions concurrently.
attackGoroutine := func(target string) {
fmt.Println("Throwing ninja stars at", target)
}
go attackGoroutine("Tommy")
time.Sleep(time.Second)
// CHANNELS
// Use channels to communicate between goroutines.
attackChannel := func(target string, attacked chan bool) {
time.Sleep(time.Second)
fmt.Println("Throwing ninja stars at", target)
attacked <- true
}
smokeSignal := make(chan bool)
evilNinja := "Tommy"
go attackChannel(evilNinja, smokeSignal)
fmt.Println(<-smokeSignal)
moreSmokeSignal := make(chan bool, 1)
moreSmokeSignal <- true
fmt.Println(<-moreSmokeSignal)
close(moreSmokeSignal)
for message := range moreSmokeSignal {
fmt.Println(message)
}
}