douba9425 2017-03-12 20:28 采纳率: 100%
浏览 242
已采纳

防止main()函数在Golang中的goroutine完成之前终止

Have loook at this contrived example:

package main

import "fmt"

func printElo() {
    fmt.Printf("Elo
")
}

func printHello() {
    fmt.Printf("Hello
")
}

func main() {
    fmt.Printf("This will print.")
    i := 0
    for i < 10 {
        go printElo()
        go printHello()
        i++
    }
}

The output of this program would be just "This will print". Output of goroutines printElo() and printHello will not be emitted because, I guess, the main() function thread will finish before the goroutines have a chance to even start executing.

What is the idiomatic way to make similar code work in Golang and not terminate prematurely?

  • 写回答

4条回答 默认 最新

  • dousuitang5239 2017-03-12 20:51
    关注

    Simplest, cleanest and "scalable" way to do it is to use a sync.WaitGroup:

    var wg = &sync.WaitGroup{}
    
    func printElo() {
        defer wg.Done()
        fmt.Printf("Elo
    ")
    }
    
    func printHello() {
        defer wg.Done()
        fmt.Printf("Hello
    ")
    }
    
    func main() {
        fmt.Printf("This will print.")
        i := 0
        for i < 10 {
            wg.Add(1)
            go printElo()
            wg.Add(1)
            go printHello()
            i++
        }
        wg.Wait()
    }
    

    Output (try it on the Go Playground):

    This will print.Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    Hello
    Elo
    

    Simple "rules" to follow when doing it with sync.WaitGroup:

    • call WaitGroup.Add() in the "original" goroutine (that starts a new) before the go statement
    • recommended to call WaitGroup.Done() deferred, so it gets called even if the goroutine panics
    • if you want to pass WaitGroup to other functions (and not use a global variable), you must pass a pointer to it, else the WaitGroup (which is a struct) would be copied, and the Done() method called on the copy wouldn't be observed on the original
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题