Slide 1

Slide 1 text

Gabriel Sobrinho . Codeminer42 . May 2020 https://github.com/sobrinho https://twitter.com/sobrinho Introduction to Go

Slide 2

Slide 2 text

Introduction • Blazing fast • Compiled • Strongly statically typed (with inference) • No exception handling • Concurrency using lightweight threads

Slide 3

Slide 3 text

package main import "fmt" func main() { fmt.Println("Hello, world!") }

Slide 4

Slide 4 text

$ go run hello.go Hello, world!

Slide 5

Slide 5 text

No exceptions!

Slide 6

Slide 6 text

Exceptions • Use multiple return values to emulate try/catch • Use defer to emulate try/catch/finally • Defer stacks functions to be called when the closest function execution is done

Slide 7

Slide 7 text

try/catch

Slide 8

Slide 8 text

func main() { data, err := ioutil.ReadFile("/tmp/file") if err != nil { fmt.Println(err.Error()) return } fmt.Println(data) }

Slide 9

Slide 9 text

$ go run read.go open /tmp/file: no such file or directory

Slide 10

Slide 10 text

$ echo "my content" > /tmp/file $ go run read.go my content

Slide 11

Slide 11 text

try/catch/finally

Slide 12

Slide 12 text

func main() { defer fn(1) fmt.Println("Hello, world!") } func fn(x int) { fmt.Println(x) }

Slide 13

Slide 13 text

$ go run defer.go Hello, world! 1

Slide 14

Slide 14 text

func main() { defer fn(1) defer fn(2) fmt.Println("Hello, world!") defer fn(3) } func fn(x int) { fmt.Println(x) }

Slide 15

Slide 15 text

$ go run defer.go Hello, world! 3 2 1

Slide 16

Slide 16 text

finally the finally

Slide 17

Slide 17 text

func main() { var file, err = os.OpenFile("/tmp/file", os.O_RDWR, 0644) if err != nil { fmt.Println(err.Error()) return } defer file.Close() var len, err = file.Write([]byte("my content")) if err != nil { fmt.Println(err.Error()) } }

Slide 18

Slide 18 text

coroutines

Slide 19

Slide 19 text

Coroutines • Lightweight threads • Used for async processing • Any function can be called in a coroutine • Communication using channels • You can wait for the result using select

Slide 20

Slide 20 text

timeout

Slide 21

Slide 21 text

func main() { tc := make(chan bool) rc := make(chan string) go timeout(tc) go read(rc) select { case word := <-rc: fmt.Println("Received", word) case <-rc: fmt.Println("Timeout.") } }

Slide 22

Slide 22 text

func read(ch chan string) { fmt.Println("Type a word, then hit Enter.") var word string fmt.Scanf("%s", &word) ch <- word } func timeout(ch chan bool) { time.Sleep(5 * time.Second) ch <- true }

Slide 23

Slide 23 text

func main() { tc := make(chan bool) rc := make(chan string) go timeout(tc) go read(rc) select { case word := <-rc: fmt.Println("Received", word) case <-tc: fmt.Println("Timeout.") } }

Slide 24

Slide 24 text

$ go run concurrency.go Type a word, then hit Enter. Timeout. $ go run concurrency.go Type a word, then hit Enter. hello Received hello

Slide 25

Slide 25 text

You can grossly compare Go's select{} with JavaScript's Promise.race()

Slide 26

Slide 26 text

There is also sync.WaitGroup if you need to wait for a specific number of coroutines to finish Another grossly comparison to JavaScript's Promise.all()

Slide 27

Slide 27 text

What have you done so far with Go? • A service that invokes a text-speech engine in macOS • A service that profile and search fingerprints using a DLL in Windows • A service that exposes local files in both platforms for reading/writing • All exposed through REST APIs in localhost

Slide 28

Slide 28 text

See the pattern? You can easily expose local assets, software and hardware, to web applications!

Slide 29

Slide 29 text

But that's not all...

Slide 30

Slide 30 text

What the community have done so far with Go? • docker - a set of tools for deploying Linux containers • kubernetes - container management system • openshift - RedHat's PaaS • terraforma - infrastructure provisioning tool • caddy - open source HTTP/2 web server with HTTPS capability • mattermost - a teamchat system

Slide 31

Slide 31 text

Who is notable using it? • Cloudflare • Dropbox • Google • Netflix • SoundCloud • Uber • And more...

Slide 32

Slide 32 text

What are you capable of doing with Go?

Slide 33

Slide 33 text

Thanks!