Is writing to two separate channels reliably sequential?


If I have a select on two channels:

for {
    select {
    case <-chan1:
        // do something
    case <-chan2:
        // do something else

And in a separate goroutine I write sequentially to those channels:

chan1 <- "blah"
chan2 <- true

Am I guaranteed to see “do something” execute before “do something else”?

I know that select chooses randomly if it has two unblocked channels, but I was thinking that writing to the first channel might reliably “interrupt” the writing goroutine if the select is already blocking, implying that the select would run on the unblocked first channel before the second write.


Yes you can rely on this if the channels are unbuffered. See the Go Memory Model.

Answered By – Volker

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.