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
Copy file name to clipboardExpand all lines: content/advent-2019/directional-channels.md
+11-6Lines changed: 11 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,9 +151,9 @@ Unlike the example above, neither timers nor tickers are ever closed to prevent
151
151
152
152
## Send-only Channels
153
153
154
-
You can also declare channels as send-only, but these are of much more limited use. While they can provide useful assertions internally that a channel is never read from, receiving them with an API is kind of backwards, and you are generally better off using a bidirectional channel internally, and moderating channel writes with a function or method.
154
+
You can also declare channels as send-only, but these are of more limited use, at least to an API. While they can provide useful assertions internally that a channel is never read from, and you should do this when you can, receiving them with an API is kind of backwards, and you are generally better off using a bidirectional channel internally, and moderating channel writes with a function or method.
155
155
156
-
Send-only channels make only one appearance in the standard library in `os/signal`:
156
+
Send-only channels make only one appearance in the API of the standard library: in `os/signal`.
157
157
158
158
```
159
159
func Notify(c chan<- os.Signal, sig ...os.Signal)
@@ -173,6 +173,11 @@ func Notify(c chan<- os.Signal, sig ...os.Signal)
173
173
It is allowed to call Notify multiple times with different channels and the
174
174
same signals: each channel receives copies of incoming signals
175
175
independently.
176
+
177
+
func Stop(c chan<- os.Signal)
178
+
Stop causes package signal to stop relaying incoming signals to c. It undoes
179
+
the effect of all prior calls to Notify using c. When Stop returns, it is
180
+
guaranteed that c will receive no more signals.
176
181
```
177
182
178
183
Here, the user is expected to pre-allocate an `os.Signal` channel for receiving incoming signals from the OS. The API asserts that the channel will only ever be written to, and informs the user that they need to create a buffered channel of whatever size they deem necessary to avoid blocking. It might seem necessary to take a send-only channel to allow the user to set their own channel depth, but the signature could just as easily have been something like:
@@ -181,13 +186,13 @@ Here, the user is expected to pre-allocate an `os.Signal` channel for receiving
181
186
func Notify(depth uint, sig ...os.Signal) <-chan os.Signal
182
187
```
183
188
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.
189
+
Which returns 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, or calling `Stop()` to cease them.
185
190
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.
191
+
This is a very specific use case, however, so you're better off finding another way to support something like this, if you can.
187
192
188
193
## Conclusion
189
194
190
-
I hope this gives advent readers a better understanding of Go's channel direction behavior and using channels to their full capabilities.
195
+
Hopefully you have a better understanding of channel directions and how they might be used, and what they might express. Thanks for following along, Advent readers!
191
196
192
197
## About the Author
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). Twitter: [@flowchartsman](https://twitter.com/flowchartsman)
198
+
Andy Walker (@flowchartsman) is a Go GDE and co-organizer of [Baltimore Go](https://www.meetup.com/BaltimoreGolang/). He is a programmer in security research for a major cybersecurity company, and 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