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

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

Golang os.stdin 作為 Goroutines 中的讀者

Golang os.stdin 作為 Goroutines 中的讀者

Go
小唯快跑啊 2021-11-22 17:43:54
在 Goroutine 中使用 os.stdin 作為 Reader 可以嗎?基本上我想要完成的是使用戶能夠在不阻塞主線程的情況下輸入消息。例子:go func() {    for {        consolereader := bufio.NewReader(os.Stdin)        input, err := consolereader.ReadString('\n') // this will prompt the user for input        if err != nil {             fmt.Println(err)             os.Exit(1)        }        fmt.Println(input)    }}()
查看完整描述

1 回答

?
莫回?zé)o

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

是的,這完全沒(méi)問(wèn)題。只要這是與 交互的唯一 goroutine os.Stdin,一切都會(huì)正常工作。


順便說(shuō)一句,您可能想要使用bufio.Scanner-使用它比使用更好一些bufio.Reader:


go func() {

    consolescanner := bufio.NewScanner(os.Stdin)


    // by default, bufio.Scanner scans newline-separated lines

    for consolescanner.Scan() {

        input := consolescanner.Text()

        fmt.Println(input)

    }


    // check once at the end to see if any errors

    // were encountered (the Scan() method will

    // return false as soon as an error is encountered) 

    if err := consolescanner.Err(); err != nil {

         fmt.Println(err)

         os.Exit(1)

    }

}()


查看完整回答
反對(duì) 回復(fù) 2021-11-22
  • 1 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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