Golang `select` looks dishonest

I am new to Golang, I read from the official spec of select that I will do a monotonous pseudo-random when more messages can go on, but when I tried the following code

package main

import (
    "fmt"
)

func main() {

    // For our example we'll select across two channels.
    c1 := make(chan string)
    c2 := make(chan string)

    go func() {
        for {
            c1 <- "one"
        }
    }()
    go func() {
        for {
            c2 <- "two"
        }
    }()

    for i := 0; i < 100; i++ {
        select {
        case msg1 := <-c1:
            fmt.Println("received", msg1)
        case msg2 := <-c2:
            fmt.Println("received", msg2)
        }
    }
}

It always prints "two received", it seems, not a random result, so where am I mistaken?

The code can be a test here .

+4
source share
2 answers

The problem is that you run it on the Go playground, on the Goa playground, GOMAXPROCS - 1. This means that one goroutine runs for a while, and if this goroutine is not blocked, the scheduler is not forced to switch to other goroutines.

, ,

, GOMAXPROCS 1 ( Go 1.5). , goroutine, , , main(), main() , ; ,

+5

Go ; . . Go 1.10 ( , Go 1.9.2, ).

0

Source: https://habr.com/ru/post/1692243/


All Articles