-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-introduction.slide
697 lines (511 loc) · 16.3 KB
/
01-introduction.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
# Introduction
Course Go
Tags: golang, go
## Outline
- Introduction to Go
- Origins
- Key characteristics & comparison with other languages
- Motivation
- IDEs and editors
- Installing Go
- Running Go
- Project basics
- Modules, Packages & Visibility
- Hello world
- Resources
## Is Go worth learning?
- Lots of interesting programming languages that you can learn
- Should you perfect them all?
- Of course no...
- So what about the Go language - is it worth a try?
- Let's take look...
## Introduction to Go
## Logo
.image assets/logos/go-logo-blue.svg _ 500
## Gophers
.image assets/gophers/go-gopher.svg _ 500
## Brand Book
- Specifies
- Designs
- Colours
- Typography
- Visual identify
_"Go is an open source programming language that enables the productions of simple, efficient and reliable software at scale."_
~ Brand Book
[Go: Brand Book](https://go.dev/assets/go-brand-book-v1.9.5.pdf)
## Origins
- Conceived late 2007 at Google
- [Rob Pike](https://en.wikipedia.org/wiki/Rob_Pike)
- [Ken Thompson](https://en.wikipedia.org/wiki/Ken_Thompson)
- [Robert Griesemer](https://en.wikipedia.org/wiki/Robert_Griesemer)
- Launched in November 2009 by Google
- Go version 1.0 released March 2012
- [Release History](https://go.dev/doc/devel/release)
- Frustration with C++ builds
- More readable replacement for languages such as C, C++ or Java
- Better suits corporations needs
- _Go vs Golang_
## Key characteristics
- _„Less is more“_
- [Open-source](https://github.com/golang/go)
- General purpose
- Procedural, C-family
- Compiled
- Statically typed
- Type inference
- Strict formatting rules
- Allows cross-compilation
- **Garbage collected**
- **Built-in concurrency**
## Influences
- Distilled existing features and mechanisms
- Mostly C family
- Inputs from Algol60, Pascal, Modula, and Oberon
[Robert Griesemer: The Evolution of Go](https://www.youtube.com/watch?v=0ReKdcpNyQg) [[Slides](https://go.dev/talks/2015/gophercon-goevolution.slide#1)]
## More readable compared to C/C++
- Declarations in postfix
- Much faster for parser to parse source code
- No semicolons
C:
```
int a;
int *p;
int *(*f)(int *);
```
[So you think you know C?](https://wordsandbuttons.online/so_you_think_you_know_c.html)
Go:
```
a int
p *int
f func(*int) *int
```
[The Go Blog: Go's Declaration Syntax](https://blog.golang.org/gos-declaration-syntax)
## More readable compared to C++
In C++ we don't say "Missing asterisk". We say:
```
error C2664:
'void std::vector<block,std::alocator<_Ty> >::push_back(const block &)':
cannot convert argument 1 from
'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<block> > >'
to 'block &&'
```
and I think that's beautiful...
[Reddit: C++ is a beautiful language](https://goo.gl/Akxjih)
## More readable compared to C++
#include <vector>
#include <algorithm>
int main()
{
int a;
std::vector< std::vector <int> > v;
std::vector< std::vector <int> >::const_iterator it = std::find( v.begin(), v.end(), a );
}
- `gcc -c error.cpp` will produce
- 15786 bytes of output
- longest line of 330 characters
## Goals
- Simplicity
- Versatility
- Unambiguity
- Performance
- Pragmatic
- Community
## Goals
- Safer applications
- Compared to C/C++
- Microservices
- Ease of install
- "DLL hell" cannot happen
- Backward compatibility
- [Compability promise](https://go.dev/doc/go1compat)
- For DevOps from DevOps
- Fast builds
- Actually super fast builds
- CI tools like it :-)
## Attention
- Go is a blend of modern approaches combined with quite old ideas
- Explicit error handling
- No generics for a long time
- Introduced in [version 1.18](https://go.dev/blog/go1.18) [March 2022]
- No class-based OOP
- No exceptions or exception handling control structures (thank god)
- I.e. `try`/`catch`/`finally` blocks
- `goto` keyword (unfortunate)
- `nil` identifier
## Go vs C/C++
- Stronger type system
- No text substitution macros
- No header files
- Package system
- Safer memory operations
- Garbage collector
- Standardized testing framework
- Stricter rules
- No pointer arithmetics
- No `void`
- ...
## We are in 21st century!
- Multi-core CPUs
- Distributed systems
- Memory locality
- Readability
##
.image assets/lecture-01/amdahls-law.png _ 650
[Wikipedia: Amdahl's law](https://en.wikipedia.org/wiki/Amdahl%27s_law) [(CC BY-SA 4.0)](https://creativecommons.org/licenses/by-sa/4.0/)
## Bit of history
## 60's
- Insufficient program flow control
- _"[Go To Statement Considered Harmful](https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf)"_
- [Edsger W. Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra)
- Solved by introducing structured programming
**=> new syntax**
## 70's
- Insufficient state control
- Solved by introducing OOP
- [Smalltalk](https://en.wikipedia.org/wiki/Smalltalk)
**=> new syntax**
## 2000's
- Insufficient concurrency control
- Not solved for a long time
- Go's goroutines and channels
- Python's and JavaScript's async
**=> new syntax**
## Concurrency and/or parallelism
- **Serial**
- Runs tasks in an order with one CPU core
- **Concurrent**
- Runs many tasks simultaneously with a less number of CPU cores (or even 1)
- **Parallel**
- Runs N tasks simultaneously with N CPU cores
## Concurrency
- When multiple tasks can run in overlapping periods
- Needs only one CPU core
- Main problem
- Interruptions
## Parallelism
- Needs more than one CPU core
- Main problem
- Isolation
- Second problem
- Coordination
## Solutions for concurrency and parallelism
- Processes
- Threads
- Coroutines
## Solutions for concurrency and parallelism
- Processes
- Threads
- **Goroutines**
- Coroutines
## CSP
- [Communicating sequential processes](https://www.cs.cmu.edu/~crary/819-f09/Hoare78.pdf) by [C.A.R. Hoare](https://en.wikipedia.org/wiki/Tony_Hoare)
- A formal language etc. etc.
- _"Don't communicate by sharing memory; share memory by communicating"_
- **Goroutines**
- **Channels**
## Memory locality
- Go:
- True structures
- True value types
- No object headers
- Java:
- No value types
- No structures
- Object headers
## Memory locality and access time
- RAM is no longer "Random Access Memory"
- We humans are bad comparing very small time periods
- Same goes for other units (length, money etc.)
.image assets/lecture-01/cpu-cache-hierarchy.png _ 500
[Andriy Berestovskyy: CPU Cache Hit Benchmark](https://www.nexthink.com/blog/smarter-cpu-testing-kaby-lake-haswell-memory)
## Popularity
## Popularity
- [Tiobe Index](https://www.tiobe.com/tiobe-index/)
- [GitHub Innovation Graph](https://innovationgraph.github.com/global-metrics/programming-languages)
- [PYPL](http://pypl.github.io/PYPL.html)
- [StackOverflow 2024 Developer Survey](https://survey.stackoverflow.co/2024/)
- [Most popular technologies](https://survey.stackoverflow.co/2024/technology/#most-popular-technologies)
- [Admired and desired](https://survey.stackoverflow.co/2024/technology/#admired-and-desired)
.image assets/gophers/hugs.png _ 240
## Use cases
## Where is it used?
- Cloud & network services
- Web development & server side applications
- CLIs
- DevOps
[Go: Use Cases](https://go.dev/solutions/use-cases)
## Cloud technologies writting in Go
- [Docker](https://www.docker.com)
- [Podman](https://podman.io)
- [Kubernetes](https://kubernetes.io)
- [Kubernetes Operators](https://coreos.com/operators)
- [MinIO](https://min.io)
- [NSQ](https://nsq.io)
- [NATS](https://nats.io)
- [OpenShift](https://www.redhat.com/en/technologies/cloud-computing/openshift)
- [Go Ethereum](https://geth.ethereum.org)
- and many more...
## Companies using Go
- Google
- Microsoft
- Amazon
- Netflix
- Meta
- Cloudflare
- Uber
- Twitch
- Dropbox
- and many more...
[Go: Case Studies](https://go.dev/solutions/case-studies)
## So...is it worth to spend time learning Go?
- Networking - **YES**
- (Micro)services - **YES**
- Scalable systems - **YES**
- You like strict formatting rules - **YES**
- You like strong type systems - **YES**
- You like minimalism - **YES**
- Pretty fast compilation - **YES**
- The simplest deployment - **YES**
- Guaranteed source code compatibility - **YES**
## So...is it worth to spend time learning Go?
- You like classic (broken) class-based OOP - **NO**
- You like baroque languages - **NO**
- Enjoy C++
- You like inconsistent languages - **NO**
- Enjoy Perl
- You like homoiconic languages - **NO**
- LISP/Scheme/Clojure are better choice
## So...is it worth to spend time learning Go?
- You want to manage memory ourself - **NO**
- You like to have buffer overflows - **NO**
- There are "better" alternatives
- You like **really very strong** type system & fast language
- Rust may be more suitable choice
[The Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/index.html)
## IDEs and editors
## IDEs and editors
- [JetBrains Goland](https://www.jetbrains.com/go/)
- [VS Code](https://code.visualstudio.com/docs/languages/go)
- Vim/Neovim with a LSP
- [gopls](https://pkg.go.dev/golang.org/x/tools/gopls#section-readme)
- [vim-go](https://github.com/fatih/vim-go)
- If you feel special or like trying something new
- [Emacs](https://www.gnu.org/software/emacs/)
- [Helix](https://helix-editor.com)
- [Zed](https://zed.dev/)
- [Sublime Text](http://www.sublimetext.com/)
[Go Wiki: Editors and IDEs](https://go.dev/wiki/IDEsAndTextEditorPlugins)
## Installing Go
## Installing Go
- The official download guide lets you download:
- _.msi_ file for Windows
- _.pkg_ file for MacOS
- _.tar.gz_ archive for Linux
- The _.msi_ and _.pkg_ can be executed directly
- The tar archive has to be unarchived, moved and the path to it exported manually
```
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go<version>linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin
```
[Go: Download and Install](https://go.dev/doc/install)
## Package Managers
Package managers can also be used
- Linux: APT
```
$ sudo apt install golang-go
```
- MacOS: [Homebrew](https://brew.sh)
```
$ brew install golang
```
- Windows: [Chocolatey](https://chocolatey.org)
```
$ choco install golang
```
## Verify Installation
Execute the `go` command to verify that go was installed and you have it in your path
```
$ go version
````
Should output:
```
go version go<version> <os>/<platform>
```
## Running Go
## Go executable
```
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
work workspace maintenance
run compile and run Go program
telemetry manage telemetry data and settings
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
```
## Workspace
- Defined by `$GOPATH` variable
- `$HOME/go` by default
- Structured directory:
- `/src`: historicaly used for storing source code
- unused after introduction of go modules
- `/pkg`: stores downloaded versioned packages
- `go get`
- `/bin`: contains executable binaries
- `go install`
## Project basics
## Keywords
```
break case chan const continue
default defer else fallthrough for
func go goto if import
interface map package range return
select struct switch type var
```
This lectures covers just two of these:
```
import
package
```
## Modules
- Introduced in Go versions 1.12-13
- Specifies project metadata and dependencies
- `go.mod`
- Dependency check-sums are saved to a separate file
- `go.sum`
- New modules can be create via `go` executable:
```
$ go mod init <name-of-module>
```
[Go Blog: Using Go Modules](https://go.dev/blog/using-go-modules)
## Modules
`go.mod`
.code go.mod /START OMIT/,/END OMIT/
`go.sum`
.code go.sum
## Packages
- Program building blocks
- Basically "libraries"
- All Go source files need to declare their package
```
package main
```
- A single directory can contain at most a single package
```
directory/
├── api.go // declares package as api - NOT ALLOWED
├── models.go // declares package as models - NOT ALLOWED
└── db.go // declares package as db - NOT ALLOWED
```
```
directory/
├── api.go // declares package as server - OK
├── models.go // declares package as server - OK
└── db.go // declares package as server - OK
```
[Go Blog: Package names](https://go.dev/blog/package-names)
## Package imports
- Packages can be imported by other programs and packages
- The packages are imported using the `import` keyword
```
import (
"fmt"
"math"
)
```
- Package name is the last element of the import path
- So reffering to the constant `Pi` from `math` package looks like this:
```
math.Pi
```
[Go Packages: Standard library packages](https://pkg.go.dev/std)
## Typical Go project layout
```
go-scrapper/
├── configs/
│ └── api.yaml
├── cmd/
│ └── scrapper/
│ └── main.go // project entrypoint
├── internal/ // packages in internal cannot be imported by outer packages
│ ├── api/
│ │ ├── handlers.go //
│ │ ├── routes.go // all files in the api directory have to share the same package
│ │ └── validation.go //
│ └── db/
│ └── repository.go
├── pkg/ // anyone can import the exported models in pkg
│ └── models/
│ ├── movie.go
│ ├── song.go
│ └── user.go
├── go.mod // project dependecies and metadata
└── go.sum // dependency check-sums
```
[GitHub: golang-standards/project-layout](https://github.com/golang-standards/project-layout)
## Executable Packages
- Must be named `main`
- Must contain `main()` function
- Program's entrypoint
## Hello world
.play assets/lecture-01/hello-world/hello-world.go
- Building an executable binary:
```
$ go build hello.go
$ ./hello
Hello world
```
- Running the code directly:
```
$ go run hello.go
Hello world
```
## Go Format
.play assets/lecture-01/hello-world/unformatted.go
- Can be formatted using the built-in `fmt` formatter
```
$ go fmt main.go
```
- Results in:
.code assets/lecture-01/hello-world/hello-world.go
## Unicode
- All string literals use UTF-8
.play assets/lecture-01/hello-world/ukrainian.go
## Visibility
- Types, variables, functions, etc. can be exported
- Every name that starts with capital letter is exported
- They can be then reached from other packages or projects
- `Pi` is a constant exported by the [math package](https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/math/const.go;l=13)
.play assets/lecture-01/visibility.go
## Resources
## Styleguides
- [Google Go Styleguide](https://google.github.io/styleguide/go/)
- [Uber Go Guide](https://github.com/uber-go/guide)
- [Effective Go](https://go.dev/doc/effective_go)
## Online resources
Starting with Go
- [Go Specification](https://go.dev/ref/spec)
- [Go Documentation](https://pkg.go.dev)
- [Go by Example](https://gobyexample.com)
- [Tour of Go](https://go.dev/tour/welcome/1)
Staying up to date
- [Golang Weekly](https://golangweekly.com)
- [Cup o' Go](https://cupogo.dev)
## Books
- [Jon Bodner: Learning Go](https://www.oreilly.com/library/view/learning-go-2nd/9781098139285/)
- [Teiva Harsanyi: 100 Go Mistakes and How to Avoid Them](https://www.manning.com/books/100-go-mistakes-and-how-to-avoid-them)
- [Online summary](https://100go.co)
## Go Playground
- [Playground](https://go.dev/play/)
- [Go Talks: History of Playground](https://go.dev/talks/2019/playground-v3/playground-v3.slide)