Golang – reading user input from stdin after reading already

huangapple go评论87阅读模式
英文:

Golang - reading user input from stdin after reading already

问题

我想能够完全从标准输入中读取内容,然后再从标准输入获取输入。我可以读取发送到我的代码的输入,但是无法读取用户输入。有没有什么办法解决这个问题?我正在编写一个分页工具时遇到了这个问题。

以下是一部分代码,它从标准输入读取内容,然后尝试读取输入。当先读取标准输入时,ReadString('\n') 不会等待,结果是一个长度为零的字符串。如果先调用 ReadString,但没有进行初始的标准输入读取,ReadString 就会正确等待。第二次调用时我得到了 EOF。我会研究一下并看看我能做些什么。

var r = bufio.NewReader(os.Stdin)

var contents string
var err error
contents, err = io.ReadAll(r)
if err != nil {
    panic(err)
}

text, err := r.ReadString('\n')
if err != nil {
    fmt.Println(err)
}
fmt.Println(text)

希望对你有帮助!

英文:

I would like to be able to read fully from stdin and then get input, also from stdin. I can read the input sent to my code but then I can't read user input. Is there any way around this? I'm doing this as part of a paging tool I am writing.

Read incoming

	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
	    sb.WriteString(fmt.Sprintf("%s\n", scanner.Text()))
	}


Reading user input

	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')

Here is part of the code that reads from stdin then tries to read from input. When stdin is read first ReadString('\n') does not wait and results in a zero length string. If ReadString is read but the initial read from stdin is not made ReadString waits properly. I get EOF in the second call. I'll look into this and see what I can do.

var r = bufio.NewReader(os.Stdin)

var contents string
var err error
contents, err = io.ReadAll(r)
if err != nil {
	panic(err)
}

text, err := r.ReadString('\n')
if err != nil {
	fmt.Println(err)
}
fmt.Println(txt)

答案1

得分: 2

我的代码首先从标准输入(stdin)读取任何输入。我使用cat命令将文本发送到程序。当我这样做时,我可以从标准输入读取输入,但如果我尝试使用reader.ReadString('\n')或fmt.Scanln()读取键入的输入,就无法获取到任何内容。

你是这样运行程序的:cat file.txt | program,然后你尝试输入更多内容并尝试读取它。

程序的标准输入(stdin)通常继承自父进程。如果你是从交互式shell运行的,那么标准输入(stdin)来自终端(即键盘)。

cat file.txt | program将标准输入(stdin)重定向为从cat的输出中读取。标准输入(stdin)不再指向终端的输入。

你需要重新打开一个与终端的流并向其写入。你可以通过读取/dev/tty来实现这一点。tty, err := os.Open("/dev/tty")

英文:

> My code first reads any input from stdin. I use cat to send text to the program. When I do that I can read the input from stdin but if I then try to read typed in input using reader.ReadString('\n') or fmt.Scanln() nothing is picked up.

You're running your program like cat file.txt | program and then you're typing more input and trying to read that, too.

A program's stdin is normally inherited from the parent process. If you're running from an interactive shell, that's input from the terminal (ie. the keyboard).

cat file.txt | program redirects stdin to read from the output of cat. Stdin no longer points at the input from the terminal.

You'll have to reopen a stream to the terminal and write to that. You do this by reading from /dev/tty. tty, err := os.Open("/dev/tty").

huangapple
  • 本文由 发表于 2022年12月27日 07:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/74924714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定