On 26 February 2013 09:26, Starfish <
ahn...@rocketmail.com> wrote:
> I've looked through Rob's excellent 'Go Concurrency Patterns', and started
> experimenting with channels.
>
> It appears that channels enable many new concepts, but those concepts are
> hard to find out about. Where can I find out more?
>
> Also, I have a few other questions, numbered for convenience. I would
> sincerely appreciate any input on any of those:
These are all excellent questions. The answers to most of them
are subjective, not universal IMHO. I'll reply with my own
take on them, which might or might not intersect with anyone else's ideas.
> 1. What is the appropriate naming convention for channels?
For a simple channel in an obvious context, "c" often suffices.
I often name a channel after the thing that's sent on it.
For instance "work" for a channel of work requests;
"done" for a channel that goroutines send on when they're done, etc.
If I need to disambiguate, I'll sometimes add a trailing "c";
for instance donec, workc. Some people prefer a trailing "Ch";
for instance doneCh, workCh.
> 2. Is it a good idea to create 'kill channels' for go routines?
If you want to kill goroutines, this can be a good idea, yes.
See also
launchpad.net/tomb, which can be useful too.
> 3. Is it better to return channels, rather than sending them as arguments?
Both ways have advantages, and both techniques are used in the
Go standard library. The place that creates the channel controls the
channel's use, so by returning a (read-only) channel you can make sure that
it's not sent to by any other goroutine. See time.After for an example
of that. If you send a channel as an argument, the caller can cause
values from several different origins to be sent to the same channel,
which can also be good, as it makes it trivial to listen for a value
from any of those origins without starting a goroutine per channel.
http://golang.org/pkg/os/signal/#Notify is one example.
> 4. Does it make sense to always start a project of non-trivial size with a
> 'for select' loop?
I don't think so. It depends very much on the project. Any given
project will probably have several such loops. That's the beauty of
Go's concurrency model: there's not necessarily a single "central loop",
just a collection of concurrently running peer goroutines that communicate
with one another. Each for-select loop is master of its own domain :-)
> 5. Should one use channels rather than callbacks?
The answer depends very much on the problem. Both are appropriate
in different places. In a sense, any place where you pass in an interface
value you're using callbacks. I tend to reserve the use of channels
for places where there's something asynchronous going on (a timer
firing, a message being received on a network connection).
In general, the decision isn't irreversible - a channel can be turned
into a callback by creating a goroutine to read from the channel
and make the call; a callback can be turned into a channel send
by making the callback send on the channel.
cheers,
rog.