它两次发布输入

时间:2011-11-28 20:35:44

标签: go

我只是想获得一些输入,在获得它后我想显示它atm我这样做:

in := bufio.NewReader(os.Stdin);
input, err = in.ReadString('\n');
if err != nil {
    fmt.Println("Error: ", err)
    os.Exit(1)
}
fmt.Printf("This is your", input)

...但是在运行它并输入一些输入后,它总是显示我的输入两次:

This is just a test
This is your This is just a test

无论如何要删除第一行?

2 个答案:

答案 0 :(得分:4)

我尚未尝试使用该软件包,但我想在这种情况下可能会有所帮助:exp/terminal。具体来说,ReadPasword函数文档是:

ReadPassword reads a line of input from a terminal without local echo.
This is commonly used for inputting passwords and other sensitive data.
The slice returned does not include the \n.

答案 1 :(得分:1)

我假设你的第一行只是你的回音输入文字?这实际上是进程终端的一个功能。由于go运行时将Stdin视为任何其他文件,因此您无法直接访问终端属性。但是,您可以使用CGO和描述here的方法来解决问题。