why golang select statement does not randomly choose a case

Issue

This is similar to go tutorial select statement, but I didn’t get answer from that post. So I asked here. Thanks for answering.

In http://tour.golang.org/concurrency/5, it seems “case c <- x:” is always ready, which means this case will not block select statement.

Based on the sentence “A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.”, when “case <-quit:” is also ready, the select statement should randomly choose from “case c <- x:” and “case <-quit:”. But the program always go into the “case <-quit:” case.

I also changed the select block to be like below. Then in the first 10 loops, the program randomly print 1-6, but the the program quits once (the 11th output) the quit channel has value 0.

My question is that if the ready cases are randomly selected from, then why the 11th selection is alway the quit case.

select {
    case c <- 1:
        x, y = y, x+y
    case c <- 2:
        x, y = y, x+y
    case c <- 3:
        x, y = y, x+y
    case c <- 4:
        x, y = y, x+y
    case c <- 5:
        x, y = y, x+y
    case c <- 6:
        x, y = y, x+y
    case <-quit:
        fmt.Println("quit")
        return
    }

Solution

On the case statements, you’re sending values to c (e.g c <- 1) which blocks until something reads off c as foo := <- c. When something writes to quit it will hit the case where <-quit is at and returning out of the select.

From this example

package main

import (
    "fmt"
    "time"
)

func main() {
    c := make(chan int)
    quit := make(chan struct{})

    go func(q chan<- struct{}) {
        time.Sleep(5 * time.Second)
        q <- struct{}{}
    }(quit)

    go func(ch chan<- int) {
        var x int
        for range time.Tick(1 * time.Second) {
            c <- x
            x++
        }
    }(c)

    for {
        select {
        case foo := <-c:
            fmt.Println("Foo", foo)
        case bar := <-c:
            fmt.Println("Bar", bar)
        case <-quit:
            fmt.Println("quit")
            return
        }
    }
}

You can see the values randomly printed out on your machine as:

$ go run foo.go
Bar 0
Bar 1
Foo 2
Bar 3
quit

$ go run foo.go
Bar 0
Foo 1
Bar 2
Bar 3
quit

Answered By – Leo Correa

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.