A little doubt about go concurrency

package main

import "fmt"

func main() {
    go loop()
    go loop()
}

func loop() {
    for i := 0; i < 10; iPP {
        fmt.Println(i)
    }
}

this is my code, and the result is nothing; as far as I understand it, it should output 0-9

on both sides.
Go
Jul.12,2022

The principle of the

Go language is that when the main function finishes, all running collaborations are terminated! That is to say, the main program has already exited, and the two function after go have not been executed yet, so you can't see the print.
you can let the main program "sleep" for a while and then quit, and then see the results!

package main

import (
   "fmt"
   "time"
)


func main() {
   go loop()
   go loop()
   time.Sleep(500000)
}

func loop() {
    for i := 0; i<10; iPP {
        fmt.Println(i)
    }
}

at least let the main go routine block and don't quit

  

when the main thread ends, the whole program ends

Menu