1 of 25

Goroutines - What, Why and How?

2 of 25

Hello!

I am Alkesh Srivastava, a machine learning engineer and a Go enthusiast

I’m here to help you understand the fundamentals of goroutines

You can find me at @_alkesh_

3 of 25

Important

4 of 25

Concurrency != Parallelism

5 of 25

What are Goroutines?

6 of 25

Goroutines are just functions that run concurrently with other functions

7 of 25

They can be compared to threads, but they are more cheaper

8 of 25

9 of 25

10 of 25

11 of 25

12 of 25

13 of 25

Code

14 of 25

first(“Wait for me”) //first starts, and we wait

second()

go first(“Proceed without me”) //first starts

second() //no need to wait for first

15 of 25

Why Goroutines?

16 of 25

I need to worry about the performance, latent deadlocks, race conditions etc. while developing concurrent applications

Java programmer

17 of 25

I need to worry about the callback spaghetti, not getting callback, getting duplicate callbacks, code readability etc.

NodeJS programmer

18 of 25

How to use Goroutines?

19 of 25

package main

import (

"fmt"

"time"

)

func hello() {

fmt.Println("Hi, I'm a goroutine!")

}

func main() {

go hello()

time.Sleep(1 * time.Second)

fmt.Println("Inside main function")

}

20 of 25

But why should I Go?

21 of 25

Native concurrency

Compiles to binary

Shallow learning curve

Built-in testing framework

Awesome community

22 of 25

And how should I Go?

23 of 25

24 of 25

Any questions?

Thank You!

25 of 25

Find me at

@_alkesh_

alkeshsrivastava.com