Concurrency
Goroutines
// Goroutines
// A goroutine is a lightweight thread managed by the Go runtime.
// Example: go f(x, y, z) starts a new goroutine running f(x, y, z).
package main
import (
"fmt"
"time"
)
// Function to be executed in a goroutine
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world") // Start a goroutine
say("hello") // Execution in the main goroutine
}Channels
Select
Range and Close
mutex
To prevent data races, we'll use a mutex to synchronize access to this shared resource.
What are two potential problems with locking a mutex?
It might block other goroutines that are also trying to lock the mutex; it could lead to deadlock.
Event loops and go-routines
Wait Groups
Last updated
Was this helpful?