是的,这完全没问题。只要这是与 交互的唯一 goroutine os.Stdin,一切都会正常工作。
顺便说一句,您可能想要使用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)
}
}()
杨魅力
相关分类