Different ways to pass channels as arguments in function

ConcurrencyGoChannel

Concurrency Problem Overview


I was reading some go code and say a few different ways to pass go channels. Maybe they are the same but I was wondering if there is any difference since I couldn't find documentation online:

func serve(ch <-chan interface{}){ //do stuff }
func serve(ch chan<- interface{}){ //do stuff }
func serve(ch chan interface{}){ //do stuff }
func server(ch *chan interface{}){ //do stuff}

I was wondering what the difference between them were and if they were just equivalent ways to do the same thing: pass a channel around different goroutines.

NOTE: I am aware that there is no reason to pass a pointer to a chan, map, or slice, or function value, since those are all reference types which internally contain a pointer (the exception would be if you want the callee to change the reference type header). The only reason I provided it is for completeness (i.e. to really provide every way a channel could be attempted to be passed as a parameter and to make on question that hopefully, references all ways to do this and compares them).

Concurrency Solutions


Solution 1 - Concurrency

I always recommend that the direction is passed everywhere it is possible, e.g.

func serve(ch <-chan SomeType) { /*do stuff*/ }

func serve(ch chan<- SomeType) { /*do stuff*/ }

By including the arrow <-chan or chan<-, you are achieving three things:

  • You are making it clear that the parameter is one end of a channel.
  • You are expressing clearly which end is being supplied. chan<- is the sending (writing) end. <-chan is the receiving (reading) end.
  • You are giving more information to the compiler for checking. If the function body tries to use the wrong end of the channel, the compiler can raise an error.

These are good reasons to show the channel end whenever possible.

Your third case depicts not specifying the end of the channel. This allows both ends of the channel to be accessed, which will be correct in some cases but in other cases may lead to accidental errors.

The fourth case, passing a pointer to a channel, is quite unusual and perhaps a bit odd. If you wanted to change the channel, it would be clearer to include it as a return parameter instead.

Solution 2 - Concurrency

The rule of thumb: Arrow shows if the data is going into (output) or going out of (input) channel. No arrow is general purpose channel.

chan <-          writing to channel (output channel)
<- chan          reading from channel (input channel)
chan             read from or write to channel (input/output channel)

Solution 3 - Concurrency

These are different types of channels. See http://golang.org/ref/spec#Channel_types . For the pointer stuff: Uncommon, but might be useful if you want to change the channel from inside the function (never saw that in the wild).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCharlie ParkerView Question on Stackoverflow
Solution 1 - ConcurrencyRick-777View Answer on Stackoverflow
Solution 2 - ConcurrencysmishraView Answer on Stackoverflow
Solution 3 - ConcurrencyVolkerView Answer on Stackoverflow