Sobes.tech
Junior — Middle

Is it possible to create a channel without specifying its size during initialization?

sobes.tech AI

Answer from AI

Yes, in Go you can create a channel without specifying its size — this will be an unbuffered channel. Such a channel blocks the sender until the receiver reads the value, and vice versa. An example of creating an unbuffered channel:

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

If you need to create a buffered channel with a specific size, you specify the second parameter:

ch := make(chan int, 10) // channel with a buffer of 10 elements

Thus, the default size of a channel is zero, and it works as a synchronization mechanism between goroutines.