Skip to content

Commit

Permalink
select: change to empty struct and close (quii#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgersh authored and quii committed Oct 25, 2019
1 parent c70cda4 commit 552bd52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions select.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,30 @@ func Racer(a, b string) (winner string) {
}
}

func ping(url string) chan bool {
ch := make(chan bool)
func ping(url string) chan struct{} {
ch := make(chan struct{})
go func() {
http.Get(url)
ch <- true
close(ch)
}()
return ch
}
```

#### `ping`

We have defined a function `ping` which creates a `chan bool` and returns it.
We have defined a function `ping` which creates a `chan struct{}` and returns it.

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.
In our case, we don't _care_ what type is sent to the channel, _we just want to signal we are done_ and closing the channel works perfectly!

Why `struct{}` and not another type like a `bool`? Well, a `chan struct{}` is the smallest data type available from a memory perspective so we
get no allocation versus a `bool`. Since we are closing and not sending anything on the chan, why allocate anything?

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

##### Always `make` channels

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.
Notice how we have to use `make` when creating a channel; rather than say `var ch chan struct{}`. 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.

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

Expand Down
4 changes: 2 additions & 2 deletions select/v2/racer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func Racer(a, b string) (winner string) {
}
}

func ping(url string) chan bool {
ch := make(chan bool)
func ping(url string) chan struct{} {
ch := make(chan struct{})
go func() {
http.Get(url)
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions select/v3/racer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func ConfigurableRacer(a, b string, timeout time.Duration) (winner string, error
}
}

func ping(url string) chan bool {
ch := make(chan bool)
func ping(url string) chan struct{} {
ch := make(chan struct{})
go func() {
http.Get(url)
close(ch)
Expand Down

0 comments on commit 552bd52

Please sign in to comment.