YouTip LogoYouTip

Go Goroutines and Channels

Goroutines

go func() {
    fmt.Println("Running in background")
}()

Channels

ch := make(chan string)

go func() {
    ch <- "Hello from goroutine"
}()

msg := <-ch
fmt.Println(msg)

Select

select {
case msg := <-ch:
    fmt.Println(msg)
case <-time.After(5 * time.Second):
    fmt.Println("Timeout")
}

Summary

  • Goroutines are lightweight threads
  • Channels synchronize goroutines
← Rust Tutorial - Getting StarteGo Functions and Error Handlin β†’