-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintroduction-to-go.slide
717 lines (485 loc) · 15.6 KB
/
introduction-to-go.slide
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# Introduction to Go
Hackers Toolbox
8 Oct 2024
Ravern Koh
NUS Hackers
@ravernkoh
## Hello, world!
Welcome to **Introduction to Go**.
By the end of this workshop, you should be able to write your own simple Go programs with confidence.
We assume that you have basic programming knowledge, like some understanding of Python for example, but not much beyond that.
## Today's Resources
.link https://goplay.tools
.link https://go.dev/play
.link https://hckr.cc/ht2425s1-w8-slides
.image images/slides-qr.png _ 350
## whoami
- I'm Ravern!
- Year 3 Computer Science student
- Interested in web development and programming languages
- Learned Go ~4 years ago
_I'm **not** an expert in Go, I'm here to teach the basics of Go. Feel free to stop me if I'm saying incorrect stuff._
## Background
.image images/gopher.png _ 100
- Designed at Google in 2009 (it's like old-new) by Robert Griesemer, Rob Pike, and Ken Thompson
- Statically-typed, garbage collected, memory-safe
- Compiles to binary executable, with strong support for cross compilation
## Why Go?
- It is _really_ simple to learn
- It provides both safety and speed
- It has built-in support for concurrency
- It has _fantastic_ tooling
## Who's using Go?
Pretty much everyone.
.image images/companies.png 320 _
It's also often used to build infrastructure tooling like **Docker** and **Kubernetes**.
## Basics of Go
## Variables
var anInt int = 42
aFloat := 3.14
var anotherInt, yetAnotherInt int = 1, 2
var (
anotherFloat float64 = 4.321
yetAnotherFloat float64 = 1.234
)
Notice that we have to declare the _type_ of each variable, e.g., `int`, `float64`.
## Primitive Data
var aBool bool = true
var anUnsignedInt uint = 42
var anInt int = -42
var aFloat float64 = 3.14
var aComplex complex64 = complex(2, 3)
var aString string = "This is a string!"
What happens if I just do this?
var aBool bool
var anInt int
## Functions
func foo() {
bar(32, "one", "two")
}
func bar(aParam int, anotherParam, yetAnotherParam string) {
// Do something...
}
func baz() (string, string) {
return "foo", "bar"
}
Notice that there isn't a type definition for `anotherParam`.
## Packages
my_project
├── server.go
├── config.go
├── main.go
├── db
│ ├── models.go
│ └── connection.go
└── auth
├── jwt.go
├── auth.go
└── decrypt.go
- Go code is written in .go files
- These files are grouped into directories
- A directory is a Go package (all files have to declare the same package name)
## Hello, world
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
## Loops
Go only has the `for` loop, but it has different kinds.
There's the Java-style for loop.
for i := 0; i < 10; i++ {
fmt.Println("for loop iteration", i)
}
And also the for-loop-that-should-really-be-a-while-loop.
j := 0
for j < 10 {
fmt.Println("while loop iteration", j)
j++
}
## Loops
There's also the infinite for loop. Usually you'd have a `break` somewhere in the loop in order to stop it.
k := 0
for {
fmt.Println("infinite loop iteration", k)
k++
}
## Conditionals
For conditionals, we have the `if` and `switch` statements.
The if statement looks pretty normal.
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is less than or equal to 5")
}
We can also initialize a variable before the condition!
if y := 1; y > 0 {
fmt.Println("y is positive")
} else {
fmt.Println("y is non-positive")
}
## Conditionals
We also have the switch statement.
switch x {
case 1:
fmt.Println("x is just 1")
case 2, 3, 4:
fmt.Println("x must be 2, 3, or 4")
fallthrough
case 5, 6, 7:
fmt.Println("x must be between 2 and 7 (inclusive)")
}
Notice how that the cases don't fall through by default, unlike in C or Java. Instead, we have to explicitly tell it to `fallthrough`.
## Conditionals
You can also have switch statements without a condition. It's equivalent to a long chain of if-else statements.
switch {
case x < 5:
fmt.Println("x is smaller than 5")
case x == 5:
fmt.Println("x is 5")
case x > 5:
fmt.Println("x is greater than 5")
}
## Pointers
Go has pointers.
Pointers store the location of the underlying value, e.g., an int pointer (`*int`) stores the location of an integer in memory.
var x, y int = 10, 20
fmt.Println("x =", x)
fmt.Println("y =", y)
var ptr *int = &x
fmt.Println("ptr =", ptr)
fmt.Println("*ptr =", *ptr)
*ptr = 30
fmt.Println("ptr =", ptr)
fmt.Println("*ptr =", *ptr)
ptr = &y
fmt.Println("ptr =", ptr)
fmt.Println("*ptr =", *ptr)
## Pointers
Passing pointers to functions allows you to modify the underlying variable.
func setToHundred(x int) {
x = 100
}
func setToHundredWithPtr(x *int) {
*x = 100
}
func main() {
x := 10
setToHundred(x)
fmt.Println("x =", x)
setToHundredWithPtr(&x)
fmt.Println("x =", x)
}
## Pointers
The zero value for pointers is `nil`.
var x *int
fmt.Println(x)
What happens when you try to modify a pointer whose value is `nil`?
## Arrays
Arrays are a collection of items that have a **fixed-size**.
var anIntArray [5]int = [5]int{1, 2, 3, 4, 5}
var aBoolArray [5]bool = [5]bool{true, false, true, false, true}
var aStringArray [5]string = [5]string{"a", "b", "c", "d", "e"}
fmt.Println("anIntArray =", anIntArray)
fmt.Println("aBoolArray =", aBoolArray)
fmt.Println("aStringArray =", aStringArray)
## Slices
Slices are a collection of items that have a no fixed size. They can be dynamically resized.
anArray := [7]int{1, 2, 3, 4, 5, 6, 7}
var aSlice []int = anArray[1:5]
fmt.Println("aSlice =", aSlice)
anotherSlice := anArray[2:6]
fmt.Println("anotherSlice =", anotherSlice)
anImmediateSlice := []int{1, 2, 3, 4, 5}
fmt.Println("anImmediateSlice =", anImmediateSlice)
## Slices
Since slices are dynamically sized, we can append to them. `append` does **not** modify the given slice but instead returns a new one.
aSlice := []int{1, 2, 3, 4, 5}
aSlice = append(aSlice, 6)
anotherSlice := append(aSlice, 7)
fmt.Println("aSlice =", aSlice)
fmt.Println("anotherSlice =", anotherSlice)
## Slices
To make an empty slice, use `make` function.
aSlice := make([]int, 10, 500)
fmt.Println("aSlice =", aSlice)
What's that second parameter (`500`) for?
## Slices
There is yet another type of for loop that can more easily loop through slices. This is the **for-range** loop.
aSlice := []int{6, 7, 8, 9, 10}
for i := range aSlice {
fmt.Println("The index is", i)
}
for i, item := range aSlice {
fmt.Println("The index is", i, "and the item is", item)
}
## Maps
Maps store key-value pairs, in order to map keys to values.
var aMap map[string]int = map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
fmt.Println("aMap =", aMap)
fmt.Println("aMap[\"one\"] =", aMap["one"])
fmt.Println("aMap[\"two\"] =", aMap["two"])
fmt.Println("aMap[\"three\"] = aMap["three"])
## Maps
We can check if keys exist in a map by getting a `bool` as a second return value.
aMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
three, ok := aMap["three"]
fmt.Println("aMap[\"three\"] =", three, ok)
four, ok := aMap["four"]
fmt.Println("aMap[\"four\"] =", four, ok)
## Maps
You can use `delete` to remove key-value pairs from maps.
aMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
delete(aMap, "three")
three, ok := aMap["three"]
fmt.Println("aMap[\"three\"] =", three, ok)
## Maps
Similar to slices, use the `make` function to create an empty map.
aMap := make(map[string]int)
aMap["one"] = 1
aMap["two"] = 2
aMap["three"] = 3
fmt.Println("aMap =", aMap)
## Maps
You can also use the range-based for loop with maps.
aMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
for key := range aMap {
fmt.Println("The key is", key)
}
for key, value := range aMap {
fmt.Println("The key is", key, "and the value is", value)
}
## Exercise
Let's write a program that counts the number of times each word in `words` appears in `passage`, and prints the counts to the console.
_Hint: Check out the `strings.Fields` function._
.link https://github.com/nushackers/intro-to-go-slides/blob/main/exercises/word_count/main.go
## Data Composition in Go
## Structs
We can use structs to group related pieces of data.
type person struct {
firstName string
lastName string
age int
}
func main() {
var p person = person{
firstName: "John",
lastName: "Doe",
age: 30,
}
fmt.Println(p)
}
## Structs
Structs can have methods, which act on the struct and can be called with the struct as the receiver.
func (p person) fullName() string {
return p.firstName + " " + p.lastName
}
func main() {
var p person = person{
firstName: "Jane",
lastName: "Doe",
age: 30,
}
fmt.Println(p.fullName())
}
## Structs
Structs are copied. This means that modifying them won't work by default.
func (p person) growOlder(years int) {
p.age += years
}
func changeFirstName(p person, newFirstName string) {
p.firstName = newFirstName
}
func main() {
var p person = person{
firstName: "Jane",
lastName: "Doe",
age: 30,
}
p.growOlder(1)
changeFirstName(p, "John")
fmt.Println(p.fullName(), "is", p.age, "years old")
}
## Structs
How do we modify them then? We can use **pointers**.
func (p *person) growOlder(years int) {
p.age += years
}
func changeFirstName(p *person, newFirstName string) {
p.firstName = newFirstName
}
func main() {
var p person = person{
firstName: "Jane",
lastName: "Doe",
age: 30,
}
p.growOlder(1)
changeFirstName(&p, "John")
fmt.Println(p.fullName(), "is", p.age, "years old")
}
## Interfaces
We can use interfaces to group related pieces of behaviour.
This interface defines a behaviour that any data that has the `area` and `perimeter` methods is a `shape`.
type shape interface {
perimeter() int
area() int
}
## Interfaces
We can then define structs that implement this behaviour.
Here's a square.
type square struct {
size int
}
func (s square) perimeter() int {
return s.size * 4
}
func (s square) area() int {
return s.size * s.size
}
## Interfaces
And here's a rectangle.
type rect struct {
length int
breadth int
}
func (r rect) perimeter() int {
return (r.length + r.breadth) * 2
}
func (r rect) area() int {
return r.length * r.breadth
}
## Interfaces
We can define a function that acts on a `shape` (and not specifically on a `square` or `rect`).
func printShape(s shape) {
fmt.Println("The area of the shape is", s.area(), "and the perimeter is", s.perimeter())
}
func main() {
s := square{size: 10}
r := rect{length: 5, breadth: 10}
printShape(s)
printShape(r)
}
## Interfaces
How do we know what's the concrete type (i.e., square, circle) stored inside the interface (i.e., shape)?
We can perform a **type switch**.
switch s.(type) {
case square:
fmt.Println("s is a square")
case rect:
fmt.Println("s is a rect")
}
## Interfaces
If we already know the concrete type, and we want to _get_ a variable of that type, we can perform a **type assertion**.
thisIsASquare, ok := s.(square)
if ok {
fmt.Println("The size of the square is", thisIsASquare.size)
}
## Exercise
Let's write a program to "process" payments from a few different payment methods.
_Don't worry there's no actual processing, just printing to the console._
.link https://github.com/nushackers/intro-to-go-slides/blob/main/exercises/payment_processing/main.go
## Concurrency and Parallelism in Go
## Goroutines
These are the unit of concurrency in Go. Spawning a new goroutine is starting a new thread of concurrent execution.
import (
"fmt"
"time"
)
func countToTen(name string) {
for i := 0; i < 10; i++ {
fmt.Println(name, "-", i)
time.Sleep(1 * time.Second)
}
}
func main() {
go countToTen("foo")
go countToTen("bar")
time.Sleep(11 * time.Second)
}
## Goroutines
Go has support for true parallelism by distributing these goroutines across multiple threads.
But as a result we can't just do operations that might be thread-unsafe. This is a much simplified example of what might go wrong.
func modifyData(data map[int]int) {
for i := 0; i < 10000; i++ {
data[0] = i
}
}
func main() {
data := make(map[int]int)
go modifyData(data)
go modifyData(data)
time.Sleep(2 * time.Second)
fmt.Println("done!")
}
## Channels
We need a safe way of passing data around without causing issues like that. This is where channels come into play.
> _Don't communicate by sharing memory; share memory by communicating ~ Rob Pike_
Channels provide a safe way to send and receive data across goroutines.
messages := make(chan string)
// in some goroutine, we send a message
messages <- message
// in another goroutine, we receive the mesasge
message := <-messages
## Channels
func readMessages(messages <-chan string) {
for m := range messages {
fmt.Println("Received message:", m)
}
}
func sendMessages(messages chan<- string) {
messages <- "Hello, world!"
time.Sleep(time.Second)
messages <- "Today we are sending and receiving messages with a channel."
time.Sleep(time.Second)
messages <- "This will be fun!"
close(messages)
}
func main() {
messages := make(chan string)
go sendMessages(messages)
readMessages(messages)
}
## Channels
We can use the `select` statement in Go to pull the first result from one or more different channels.
c1 := make(chan string)
c2 := make(chan string)
select {
case msg1 := <-c1:
fmt.Println("received from channel 1")
case msg1 := <-c2:
fmt.Println("received from channel 2")
}
## Channels
What happens when we send a value to a channel that no one's receiving on?
**We can't do that.** Or rather, the goroutine trying to send a message will just block until another goroutine chooses to receive from that channel.
This could be quite inconvenient behaviour, since we kind of want the channel to store these values for later use.
We can use **buffered channels** for this purpose.
messages := make(chan string, 5)
This channel can store 5 messages. The 6th one will still block the goroutine.
## Exercise
Let's write a program to "download" videos concurrently.
But there's a catch! You can only download up to a certain number of videos at the same time, otherwise the program will crash.
Obviously, you shouldn't be downloading them one-by-one either.
.link https://github.com/nushackers/intro-to-go-slides/blob/main/exercises/video_downloading/main.go
## What's Next
- Generics (this is a somewhat new thing)
- Organising many files into an app
- Dependency management