Select within goroutine evaluates every other statement

In the uppermost snippet, you're pulling two values from the channel for each loop. One in the select statement and one in the print statement.

Change

        select {
        case <-a:
            fmt.Print(<-a)

To

        select {
        case val := <-a:
            fmt.Print(val)

http://play.golang.org/p/KIADcwkoKs


<-a

gets a value from the channel, destructively. So in your code you get two values, one in the select statement, and one to print. The one received in the select statement is not bound to any variable, and is therefore lost.

Try

select {
    case val := <-a:
        fmt.Print(val)

instead, to get only one value, bind it to variable val, and print it out.

Tags:

Concurrency

Go