Sobes.tech
Junior — Middle

How is data transmitted through a non-buffered channel?

sobes.tech AI

Answer from AI

In Go, an unbuffered channel provides synchronous data transfer between goroutines. This means that sending a value into the channel blocks until another goroutine receives it, and vice versa — receiving blocks until a value is available for reading.

Thus, data transfer through an unbuffered channel occurs with sender and receiver synchronization, guaranteeing data exchange without intermediate storage.

Example:

ch := make(chan int) // unbuffered channel

go func() {
    ch <- 42 // send blocks until someone reads
}()

value := <-ch // receive blocks until a value appears
fmt.Println(value) // will print 42

In this example, the goroutine sends a number into the channel and blocks until the main goroutine reads it.

How is data transmitted through a non-buffered… - sobes.tech