The big deal about concurrency in Go
March 28, 2021
Goroutines
Goroutines sind leichte Threads, gesteuert vom Go Runtime Scheduler.
code
package main
import (
"fmt"
"time"
)
func hello() {
fmt.Println("Hello goroutine")
}
func main() {
go hello()
time.Sleep(1 * time.Second)
fmt.Println("main done")
}
Channels (Sync)
code
ch := make(chan string)
go func() { ch <- "done" }()
msg := <-ch
fmt.Println(msg)
Warum wichtig?
- Einfache Concurrency ohne Thread Management
- Gut fuer I/O-heavy Workloads (APIs, Queues)