←back to thread

159 points mpweiher | 1 comments | | HN request time: 0.22s | source
Show context
politician ◴[] No.43672510[source]
My rule of thumb is that the goroutine that writes to a channel is responsible for closing it. In this case, a deferred call to close the channel in HandlePlayer is sufficient.

Still, this example has other issues (naked range over a channel?!) potentially contributing to the author’s confusion.

However, this post was also written almost a decade ago, so perhaps it’s a result of being new to the language? If I cared to look, I’d probably be able to find the corresponding HN thread from that year full of arguments about this, hah.

replies(2): >>43672690 #>>43673504 #
1. sateesh ◴[] No.43672690[source]
> In this case, a deferred call to close the channel in HandlePlayer is sufficient

It is not clear from the example, but I presume there would multiple players, i.e there will calls of the form:

  g.HandlePlayer(p1)
  g.HandlePlayer(p2)
  ..
in such a case one player closing the channel would affect rest of the producers too.