Go 中 defer 会不会等待 goroutinue ,小例子

package main

import (
    "fmt"
    "sync"
)

func main()  {
    defer func() {
        fmt.Println("defer")
    }()
    var wg sync.WaitGroup

    wg.Add(1)
    go func(wg *sync.WaitGroup) {
        defer wg.Done()
        //time.Sleep(1000000)
        fmt.Println("gogoggogogo")
    }(&wg)
    wg.Wait()
}

结果:
gogoggogogo
defer

 

posted @ 2021-01-08 11:55  Black_Climber  阅读(88)  评论(0编辑  收藏  举报