Hello. Below is an example of the code, please suggest why the second gorutina writes to the channel faster: https://play.golang.org/p/olYKJ08L5C

c := make(chan string) go func(s string){ c <- s }("first") go func(s string){ c <- s }("second") fmt.Println(<-c, <-c) 

    1 answer 1

    The order of the gorutin is not defined. When you do go f(a) , there is no guarantee when f(a) will start. See for example the document The Go Memory Model , which describes what happens next.

    If you need a clear order, then firstly, it is not clear why you need gorutiny, and secondly, you need to prescribe a synchronization mechanism.

    • Sample code is taken from go-tour-ru-ru.appspot.com/concurrency/2. I want to understand why Gorutin, who is declared the second, writes to the channel faster and also if you swap them, the first will write to the channel faster. As I understand it, the first Gorutina must capture the channel and write the data there, but the second one cannot write anything there until the data has been read from the channel - Sandman
    • @Sandman Once again, the order of launching gorutin does not matter, because it is not defined. In one version, the second one is launched before the first, in the other - on the contrary, in the third one - randomly. Your task is to create programs that are not tied to the order. - Ainar-G
    • Thanks so much) - Sandman