【问题标题】:How do I make a GO program wait until there is user input? [duplicate]如何让 GO 程序等到有用户输入? [复制]
【发布时间】:2015-11-21 02:24:04
【问题描述】:
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    fmt.Println("insert y value here: ")
    input := bufio.NewScanner(os.Stdin)
    fmt.Println(input.Text)
}

如何让程序等待,直到用户输入数据?

【问题讨论】:

  • 请不要使用行号。它使人们很难复制和粘贴您的程序来重现您的问题。

标签: go


【解决方案1】:

Scanner 对于读取命令行输入并不是很理想(请参阅上面引用的答案 HectorJ),但如果你想让它工作,它是你缺少的对 Scan() 的调用(另请注意 Text( ) 是一个方法调用):

func main() {
    fmt.Print("insert y value here: ")
    input := bufio.NewScanner(os.Stdin)
    input.Scan()
    fmt.Println(input.Text())
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-13
    • 2020-05-04
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多