Senior
What can be used to pass data between goroutines?
sobes.tech AI
Answer from AI
In Go, channels are commonly used for data transfer between goroutines. Channels provide safe synchronization and data transfer, allowing goroutines to exchange values without explicit locks.
Example of using a channel:
ch := make(chan int)
// Sending data in a goroutine
go func() {
ch <- 42
}()
// Receiving data
value := <-ch
fmt.Println(value) // 42
Besides channels, shared memory with synchronization via mutex from the sync package can be used, but channels are considered a more idiomatic way in Go.