Skip to content

Commit ef6b317

Browse files
committed
explain nil channels to fix #220
1 parent 00a5f1e commit ef6b317

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

select.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,19 @@ func ping(url string) chan bool {
224224

225225
#### `ping`
226226

227-
We have defined a function `ping` which creates a `chan bool` and returns it.
227+
We have defined a function `ping` which creates a `chan bool` and returns it.
228228

229229
In our case, we don't really _care_ what the type sent in the channel, _we just want to send a signal_ to say we're finished so booleans are fine.
230230

231231
Inside the same function, we start a goroutine which will send a signal into that channel once we have completed `http.Get(url)`.
232232

233+
##### Always `make` channels
234+
235+
Notice how we have to use `make` when creating a channel; rather than say `var ch chan bool`. When you use `var` the variable will be initialised with the "zero" value of the type. So for `string` it is `""`, `int` it is 0, etc.
236+
237+
For channels the zero value is `nil` and if you try and send to it with `<-` it will block forever because you cannot send to `nil` channels
238+
239+
[You can see this in action in The Go Playground](https://play.golang.org/p/IIbeAox5jKA)
233240
#### `select`
234241

235242
If you recall from the concurrency chapter, you can wait for values to be sent to a channel with `myVar := <-ch`. This is a _blocking_ call, as you're waiting for a value.

0 commit comments

Comments
 (0)