Junior — Middle
Milliseid paralleelsete ülesannete realiseerimise tüüpe teate?
sobes.tech AI
Vastus AI-lt
In Go, parallel tasks can be implemented in several ways:
-
Goroutines — lightweight threads started with the keyword
go. They allow functions to run in parallel. -
Channels — used for communication and synchronization between goroutines, enabling safe data transfer.
-
Worker pools — a group of goroutines that take tasks from a queue and process them, helping to control the number of goroutines running simultaneously.
-
sync.WaitGroup — to wait for a group of goroutines to finish.
Example of starting a goroutine and passing data through a channel:
ch := make(chan int)
// Start a goroutine
go func() {
ch <- 42 // send value to channel
}()
// Receive value from channel
val := <-ch
fmt.Println(val)