Câu hỏi phỏng vấn Golang
Câu hỏi

What are the differences between unbuffered and buffered channels?

Câu trả lời

In Go programming, channels are used for communication between goroutines, which are lightweight threads managed by the Go runtime. The differences between unbuffered and buffered channels primarily revolve around how they handle synchronization and capacity, affecting the behavior of goroutines during send and receive operations.

Unbuffered Channels

Unbuffered channels have no capacity to store messages. This means that a send operation on an unbuffered channel will block the sending goroutine until another goroutine is ready to receive the message. Similarly, a receive operation will block until there is data to receive. Thus, communication via an unbuffered channel requires both the sender and receiver to be ready to perform their respective operations at the same time, ensuring direct hand-off of the message. This synchronization guarantees that the send and receive operations are perfectly coordinated, with no intermediate storage of the message[1][2][3][4][5].

Buffered Channels

Buffered channels, on the other hand, have a specified capacity. This capacity allows them to store one or more messages before blocking the sending operation. A send operation on a buffered channel will only block if the channel's buffer is full. Conversely, a receive operation will block if the channel is empty. Buffered channels thus provide more flexibility by decoupling the send and receive operations; the sender does not need to wait for a receiver to be ready if there is still space in the buffer[1][2][3][4]...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between C.sleep() and time.Sleep() ?

middle

Is Go an object-oriented language?

junior

Does Go have exceptions?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào