1 回答

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)
}
}()
- 1 回答
- 0 關(guān)注
- 198 瀏覽
添加回答
舉報(bào)