Junior — Middle
Kuidas toimub andmete edastamine mitte-puhverdatud kanalil?
sobes.tech AI
Vastus AI-lt
In Go, an unbuffered channel (without buffer) provides synchronized data transfer between goroutines. This means that sending a value to the channel blocks until another goroutine receives that value from the channel, and vice versa — receiving blocks until there is a value to read.
Thus, data transfer through an unbuffered channel occurs with synchronization of sender and receiver, guaranteeing data exchange without intermediate storage.
Example:
ch := make(chan int) // unbuffered channel
go func() {
ch <- 42 // sending blocks until someone reads
}()
value := <-ch // receiving blocks execution until a value appears
fmt.Println(value) // will print 42
In this example, the goroutine sends a number to the channel and blocks until the main goroutine reads that number.