You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the declaration format most people first encounter when working with channels, and any channel created in this way will be *bidirectional*, which means that anyone who has access to a channel value can read from it *and* write to it. This can cause problems in a concurrent environment, and many a Go programmer has torn hair from their heads trying to debug a `panic: send on a closed channel`.
21
+
This is the declaration format most people first encounter when working with channels. But any channel created in this way will be bidirectional as the default behavior. This means that anyone who has access to a channel value can read from it *and* write to it. This can cause problems in a concurrent environment, and many a Go programmer has torn hair from their heads trying to debug a `panic: send on a closed channel`.
22
22
23
23
The common wisdom is that only the sender should close a channel, and this makes sense. Only the sender can know when there's no more data to send, and it's the receiver's responsibility to watch for the close, or ideally, to simply `range` over the channel, exiting the loop naturally when it's done. If this order is upset, it's generally a sign something very wrong is going on, hence the panic. But if anyone can perform any action on a channel, including calling `close()`, how can you reel this in?
24
24
@@ -181,9 +181,13 @@ Here, the user is expected to pre-allocate an `os.Signal` channel for receiving
181
181
func Notify(depth uint, sig ...os.Signal) <-chan os.Signal
182
182
```
183
183
184
-
Returning a receive-only channel, similarly to how package `time` operates. The only difference is that, by taking a channel as an argument package `os/signal` can keep track of the user's notify channels, allowing for the multiple calls it mentions to expand the set of signals the channel will receive.
184
+
Returning a receive-only channel, similarly to how package `time` operates. The only difference is by taking a channel as an argument, package `os/signal` can keep track of the user's notify channels, allowing for the multiple calls it mentions to expand the set of signals the channel will receive.
185
185
186
186
This is a very specific use case, however, and one that involves a global state, so you're better off finding another way to support something like this, if that's your goal.
187
187
188
+
## Conclusion
189
+
190
+
I hope this gives advent readers a better understanding of Go's channel direction behavior and using channels to their full capabilities.
191
+
188
192
## About the Author
189
193
Andy Walker is a Go GDE and co-organizer of [Baltimore Go](https://www.meetup.com/BaltimoreGolang/), as well as the primary orgnizer of the GopherCon Guide Program. He is a programmer in security research for a major cybersecurity company. He enjoys hardware, 3D printing, and talking way too much about philosophy. He can be reached at andy-at-[andy.dev](https://andy.dev).
0 commit comments