'priority rule between sent and receive operation in a go select statement

Is there a priority rule between sent and receive operation in a go select statement ?

Since a "send" operation is always ready, not like a "receive" operation that wait for something to come from the channel, I always have the feeling that the "send" will be executed first on a select.

I tried a little code to test what happens when both send and receive are ready:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)
    go goOne(ch1)
    go goTwo(ch2)

    time.Sleep(time.Second * 2)
    select {
    case ch2 <- "To goTwo goroutine":
    case msg1 := <-ch1:
        fmt.Println(msg1)
    }
}

func goOne(ch chan string) {
    ch <- "From goOne goroutine"
}

func goTwo(ch chan string) {
    msg := <-ch
    fmt.Println(msg)
}

The result seems to always be "From goOne goroutine". So it seems the receive operation has the priority. But is it by design effect ? Or could it happen that the sent got executed first? I couldn't find the info in the doc

If I want the receive operation to have the priority, can I rely on that or should I do something else ?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source