为什么主goroutine总是第二个被调用

package main
import (
  "sync"
  "time"
  )
func main() {
  var wg sync.WaitGroup

  wg.Add(1)

  go func() {         //A
    wg.Wait()
    println("wait exit")
  }()

  go func() {
    time.Sleep(time.Second)
    wg.Done()
  }()

  wg.Wait()
  println("main exit")
}

result:

wait exit
main exit

Why don't main goroutine execute println("main exit") first, and main thread dead and then discard A goroutine? It keeps printing like the result shows

Chance.

There's nothing in the language spec that says your "wait exit" should execute either before or after "main exit".

Chances are if you run the program enough times, sometimes "main exit" will run first. But also maybe not. The results are not defined, and depend on the runtime state and implementation. As such results may even change between Go versions.