英文:
It's posting the input twice
问题
我想从stdin
读取一些输入,然后显示它。目前我是这样做的:
in := bufio.NewReader(os.Stdin);
input, err = in.ReadString('\n');
if err != nil {
fmt.Println("错误:", err)
os.Exit(1)
}
fmt.Printf("这是你的", input)
...但是在运行这个程序并输入一些内容后,它总是显示我的输入两次,像这样:
这只是一个测试
这是你的这只是一个测试
有没有办法去掉第一行?
英文:
I want to read some input from stdin
and then display it. At the moment I am doing this like this:
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)
...but after running this and entering some input it is always displaying my input twice like this:
This is just a test
This is your This is just a test
Is there anyway to remove the first line?
答案1
得分: 4
我还没有尝试过这个包的任何功能,但我猜它在这种情况下可能会有帮助:exp/terminal。具体来说,ReadPassword 函数的文档如下:
ReadPassword 从终端读取一行输入,但不会回显。这通常用于输入密码和其他敏感数据。返回的切片不包括 \n。
英文:
I haven't yet tried anything with the package, but I guess it could be helpful in this case: exp/terminal. Specifically the ReadPasword function documentations is:
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.
答案2
得分: 1
我假设你的第一行只是你回显的输入文本?这实际上是进程终端的一个功能。由于Go运行时将Stdin视为任何其他文件一样处理,因此您无法直接访问终端属性。但是,您可能可以使用CGO和这里描述的方法来拼凑一些东西。
英文:
I assume your first line is just your echoed input text? That's actually a function of the process' terminal. Since the go runtime treats Stdin like any other file, you don't have direct access to the terminal attributes. However you might be able to hack something together with CGO and the approach described here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论