Goroutines - What, Why and How?
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_
Important
Concurrency != Parallelism
What are Goroutines?
Goroutines are just functions that run concurrently with other functions
They can be compared to threads, but they are more cheaper
Code
first(“Wait for me”) //first starts, and we wait
second()
go first(“Proceed without me”) //first starts
second() //no need to wait for first
Why Goroutines?
I need to worry about the performance, latent deadlocks, race conditions etc. while developing concurrent applications
Java programmer
I need to worry about the callback spaghetti, not getting callback, getting duplicate callbacks, code readability etc.
NodeJS programmer
How to use Goroutines?
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")
}
But why should I Go?
Native concurrency
Compiles to binary
Shallow learning curve
Built-in testing framework
Awesome community
And how should I Go?
Any questions?
Thank You!
Find me at
@_alkesh_
alkeshsrivastava.com