select with single case blocks, adding default: unblocks

1- When you are dealing with one channel, it is OK to use for,
consider this working code ( The Go Playground ):

package main

import "fmt"

func main() {
    ch := make(chan int, 2)
    ch <- 1
    ch <- 2
    close(ch)
    for range ch {
    }
    fmt.Println("Done.")
}

This will empty the channel.
Note: you should close the channel or you should use break statement to finish that loop.


2- When you are dealing with more channels you may use select, like this ( The Go Playground ):

for {
    select {
    case <-pause:
        fmt.Println("pause")
        select {
        case <-play:
            fmt.Println("play")
        case <-quit:
            wg.Done()
            return
        }
    case <-quit:
        wg.Done()
        return
    default:
        work()
    }
}

3- Using nil and closed channel ( The Go Playground ):

package main

import "fmt"

func main() {
    var quit chan struct{} // nil

    select {
    case <-quit:
        fmt.Println("1")
    default:
        fmt.Println("2") // This runs
    }

    quit = make(chan struct{}, 1)

    select {
    case <-quit:
        fmt.Println("10")
    default:
        fmt.Println("20") // This runs
    }

    quit <- struct{}{} // send

    select {
    case <-quit:
        fmt.Println("100") // This runs
    default:
        fmt.Println("200")
    }

    close(quit)

    select {
    case <-quit:
        fmt.Println("1000") // This runs
    default:
        fmt.Println("2000")
    }

    select {
    case <-quit:
        fmt.Println("10000") // This runs
    default:
        fmt.Println("20000")
    }
}

output:

2
20
100
1000
10000

Select statements

A "select" statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations.

A case with a RecvStmt may assign the result of a RecvExpr to one or two variables, which may be declared using a short variable declaration. The RecvExpr must be a (possibly parenthesized) receive operation. There can be at most one default case and it may appear anywhere in the list of cases.

Execution of a "select" statement proceeds in several steps:

For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed. Unless the selected case is the default case, the respective communication operation is executed. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned. The statement list of the selected case is executed. Since communication on nil channels can never proceed, a select with only nil channels and no default case blocks forever.


In your case, it seems that a simple loop would suffice:

for _ = range ch {
    fmt.Println("drain")
}

Select is required, if you have multiple goroutines to handle. The tour has an example of range and close.

Another case for select would be idempotent channel closes: https://play.golang.org/p/_Ol42BvuuS.

Tags:

Go