

Advanced Go Concurrency Patterns [video] - enneff
http://blog.golang.org/2013/05/advanced-go-concurrency-patterns.html

======
mitchellh
The talk is worth watching, but is based on a single big "trick" (but really
language feature).

The key thing shown in this talk to unlock all the other cool patterns is that
a nil channel blocks indefinitely in a select statement, meaning it is never
selected. So you can set channels to nil in order to "turn off" certain code
paths in a select. Example:

    
    
        var myChan chan int
        if someCondition {
          // Turn on case by setting the channel
          myChan = someChannel
        }
    
        select {
        case <-myChan:
          // Do a thing. Will never execute if myChan is nil.
        // ... etc.
        }

~~~
latch
Can't pass up a select without showing a timeout. This was an "ah ha" moment
for me and go:

    
    
        select {
        case <-myChan:
          // Do a thing
        case <- time.After(10 * time.Second):
          // default/error whatever
        }

~~~
jzelinskie
You can also use time.Tickers to schedule events to occur every so often in a
similar fashion.

    
    
         for now := range time.NewTicker(time.Minute).C {
           fmt.Printf("%s is the current time\n", now)
         }
    

In case you are unfamiliar, the range keyword, when used on a channel, is
equivalent to:

    
    
         for {
           select {
           case now := <-time.NewTicker(time.Minute).C:
             fmt.Printf("%s is the current time\n", now)
           }
         }

~~~
zimbatm
On the second example, wouldn't it make sense to create the ticker outside of
the for loop instead ?

~~~
jzelinskie
Yep, that was my bad. I got distracted when writing the example and didn't
think things through because I realized I had installed Go 1.1 for i386 on my
laptop and not x86_64. Such is life when commenting at 6AM :P

------
binarycrusader
Exactly what I've been looking for. The original presentation Pike gave was
great as an intro, but I was looking for some more applications. Definitely
looking forward to more material like this.

Also, I love the tongue-in-cheek reference to what I presume is Google Reader:

    
    
      Example: feed reader
      My favorite feed reader disappeared. I need a new one.
      Why not write one?
      Where do we start?

------
tiziano88
I can't even go past the first slide from my phone and tablet :/ apparently
you need arrow keys to move to the next slides.....

~~~
shawabawa3
You can also click on the edges of the page (works for me on a pc anyway)

------
tharshan09
Really interesting. Could someone explain how these features relate in a
language like python or C? Do they have the same features, but are more
complicated to implement or is the architecture not even there to support
such?

~~~
dripton
Neither language has channels or selects baked in.

Python does have threadsafe queues in the standard library, which you can use
to coordinate work between threads and processes.

C doesn't come with anything like this, but you can use a library like ZeroMQ.
The main Go compilers are written in C, so clearly anything Go can do C can do
too -- but it'll be more work.

