[Golang] Understanding Unbuffered and Buffered Channels

Table of contents

Channel capacity
Behavior
Closed channel
Receive-Only & Send-only Channel
References

Channel capacity

Unbuffered channel:

Has no capacity.
You can imagine it like a highway that allows one car to run …


This content originally appeared on DEV Community and was authored by JackTT

Table of contents

  • Channel capacity
  • Behavior
  • Closed channel
  • Receive-Only & Send-only Channel
  • References

Channel capacity

  • Unbuffered channel:
    • Has no capacity.
    • You can imagine it like a highway that allows one car to run throungh but does not support any cars to park there.
  • Buffered channel:
    • Has a fixed capacity at declaration time.
    • It's like a queue that supports holding up to a fixed number of pending messages before consuming.

Behavior

  • Unbuffered channel:

    • Sender is blocked until a receiver receive the message.
    • Receiver is blocked until a sender sends a message.
    • The sending and receiving processes are synchronous.
  • Buffered channel:

    • Senders are blocked only when the buffer is full.
    • Receivers are blocked when the buffer is empty.
    • The sending and receiving processes are asynchronous. Senders are released immediately after the sending action without waiting for the receiver.

Worth noting: An unbuffered channel differs from a buffered channel with a size of 1 according to the behavior described above.

Closed channel

When a channel is closed in Go using the close() function:

  • Sending to a closed channel causes a panic.
  • Receiving from a closed channel continues to retrieve values until the buffer is drained (for buffered channels). After the buffer is empty, further receives return the zero value of the channel's type and do not block.

You can check if a channel is closed by receiving a second value from the channel, e.g., v, ok := <-ch. The ok will be false if the channel is closed.

Receive-Only & Send-only Channel

Receive-only channel in Go is a channel that can only be used to receive values.

  • Declaration: var ch <-chan int defines a channel from which only receiving is allowed.
  • Behavior:
    • Attempting to send to a read-only channel will result in a compilation error.
    • Attempting to pass a receive-only channel into a function that requires normal channel also lead to a compilation error.

Send-only channel is similar.

var ch chan<- int

References


This content originally appeared on DEV Community and was authored by JackTT


Print Share Comment Cite Upload Translate Updates
APA

JackTT | Sciencx (2024-09-14T08:07:26+00:00) [Golang] Understanding Unbuffered and Buffered Channels. Retrieved from https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/

MLA
" » [Golang] Understanding Unbuffered and Buffered Channels." JackTT | Sciencx - Saturday September 14, 2024, https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/
HARVARD
JackTT | Sciencx Saturday September 14, 2024 » [Golang] Understanding Unbuffered and Buffered Channels., viewed ,<https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/>
VANCOUVER
JackTT | Sciencx - » [Golang] Understanding Unbuffered and Buffered Channels. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/
CHICAGO
" » [Golang] Understanding Unbuffered and Buffered Channels." JackTT | Sciencx - Accessed . https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/
IEEE
" » [Golang] Understanding Unbuffered and Buffered Channels." JackTT | Sciencx [Online]. Available: https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/. [Accessed: ]
rf:citation
» [Golang] Understanding Unbuffered and Buffered Channels | JackTT | Sciencx | https://www.scien.cx/2024/09/14/golang-understanding-unbuffered-and-buffered-channels/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.