Junior — Middle
What methods are used for interaction between goroutines in programming language?
sobes.tech AI
Answer from AI
In the Go language, interaction between goroutines is usually done through channels. Channels allow safe data transfer between goroutines, providing synchronization and message exchange.
Example of using a channel:
package main
import (
"fmt"
)
func worker(ch chan string) {
ch <- "done"
}
func main() {
ch := make(chan string)
go worker(ch)
msg := <-ch
fmt.Println(msg)
}
Besides channels, other synchronization mechanisms can be used, such as mutexes from the sync package, but channels are considered the idiomatic way of communication between goroutines in Go.