Junior — Middle
Kas saaksid loetleda sidekanalite peamised omadused ja parameetrid?
sobes.tech AI
Vastus AI-lt
In Go, channels are a means for transmitting data between goroutines, providing synchronization and communication. The main properties and parameters of channels:
- Data type: the channel transmits values of a specific type, for example,
chan intfor integers. - Buffering: channels can be buffered or unbuffered. An unbuffered channel blocks the sender until the receiver reads the value. A buffered channel has capacity, allowing multiple values to be sent without blocking.
- Direction: a channel can be bidirectional (
chan T), or unidirectional — only for sending (chan<- T) or only for receiving (<-chan T).
Example of creating a buffered channel:
ch := make(chan int, 5) // channel with buffer for 5 elements