第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用等待組來完成 goroutine

使用等待組來完成 goroutine

Go
瀟瀟雨雨 2023-08-07 10:37:31
我已將以下內(nèi)容添加到 stdout 和 stderr goroutine (go func() …) 以等待獲取output并error完成?,F(xiàn)在我希望外部函數(shù)將等待兩個(gè)例程完成。func exec(stdout io.Reader, stderr io.Reader) (*bufio.Scanner, *bufio.Scanner) {scanout := bufio.NewScanner(stdout)scanout.Split(bufio.ScanRunes)go func() {    for scanout.Scan() {        fmt.Print(scanout.Text())     }}()go func() {scanerr.Split(bufio.ScanRunes)        for scanerr.Scan() {            fmt.Print(scanerr.Text())        }}()}現(xiàn)在我嘗試添加等待組,但它不起作用,因?yàn)槲也淮_定如何傳遞等待組實(shí)例,知道嗎?func exec(stdout io.Reader, stderr io.Reader) (*bufio.Scanner, *bufio.Scanner) {scanout := bufio.NewScanner(stdout)scanout.Split(bufio.ScanRunes)var waitgroup sync.WaitGroupwaitgroup.Add(1)go func() {    for scanout.Scan() {        fmt.Print(scanout.Text())     }}()waitgroup.Wait()waitgroup.Add(1)go func() {scanerr.Split(bufio.ScanRunes)        for scanerr.Scan() {            fmt.Print(scanerr.Text())        }}()waitgroup.Wait()}更新應(yīng)該是這樣嗎?func exec(stdout io.Reader, stderr io.Reader) (*bufio.Scanner, *bufio.Scanner) {scanout := bufio.NewScanner(stdout)scanout.Split(bufio.ScanRunes)var waitgroup sync.WaitGroupwaitgroup.Add(2)go func() {    for scanout.Scan() {        fmt.Print(scanout.Text())     }}()go func() {scanerr.Split(bufio.ScanRunes)        for scanerr.Scan() {            fmt.Print(scanerr.Text())        }}()waitgroup.Wait()}
查看完整描述

1 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

您不需要將WaitGroup實(shí)例傳遞給 goroutine,因?yàn)槟褂玫氖呛瘮?shù)文字,您可以直接引用在周圍函數(shù)中聲明的變量。

您也可以省略第二個(gè) goroutine,它對于您的用例來說并不是真正需要的。

func exec(stdout io.Reader, stderr io.Reader) (*bufio.Scanner, *bufio.Scanner) {

? ? scanout := bufio.NewScanner(stdout)

? ? scanout.Split(bufio.ScanRunes)


? ? var wg sync.WaitGroup

? ? wg.Add(1)


? ? // exec scanout in its own goroutine

? ? go func() {

? ? ? ? for scanout.Scan() {

? ? ? ? ? ? fmt.Print(scanout.Text())

? ? ? ? }

? ? ? ? wg.Done()

? ? }()


? ? // exec scanerr

? ? scanerr.Split(bufio.ScanRunes)

? ? for scanerr.Scan() {

? ? ? ? fmt.Print(scanerr.Text())

? ? }


? ? // wait for scanout

? ? wg.Wait()

}


查看完整回答
反對 回復(fù) 2023-08-07
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號