Junior — Middle
Explain the principle of data transfer channels in your application.
sobes.tech AI
Answer from AI
In Go, a channel is a type that allows safe data transfer between goroutines, providing synchronization and message exchange.
A channel can be thought of as a queue or pipeline where one goroutine places data and another retrieves it. This helps avoid issues with concurrent access to shared data.
Example of using a channel:
ch := make(chan int) // create a channel for transmitting int
// sending data to the channel
go func() {
ch <- 42
}()
// receiving data from the channel
value := <-ch
fmt.Println(value) // will output 42
Thus, a channel ensures thread-safe data transfer and synchronization between goroutines.