golang reading long text from stdin

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

golang reading long text from stdin

问题

我想从os.Stdin读取一段长文本,但我无法实现。已经阅读了主题中的所有内容,尝试了应该有效的代码。无论如何,每种方法都会在4096个字符后截断。

例如,这里有一个有效的示例。在循环的第一次运行之后,它读取了前4096个字符,然后等待更多的输入,直到我用EOF(Ctrl+D)结束。对于fmt.Scan、bufio.NewScanner、bufio.ReadLine、ioutil.ReadAll都是一样的。如果我将其保存到文件中并读取它,它会按预期工作。但从标准输入读取时却不行。

我使用的是Arch Linux,32位,Go 1.7,在mate-terminal 1.14和tty 8.25中进行了测试,两者都出现了同样的问题。在hackerrank.com页面上也是一样,我不知道他们在使用什么技术。

请帮忙!

编辑:

我的输入只比4096个字符长一点点。我查看了Amd分享的链接,得到了以下结论:我的输入只是包含以空格分隔的整数的一行。当我将空格改为换行符时,它可以工作。但由于hackerrate上的练习格式使用了很长的以空格分隔的行,问题仍然存在,只是稍微改进了一下。

英文:

I want to read a long text from os.Stdin, but I can't make it happen. Already read everything in the subject, tried codes that supposed to work. Every method cuts after 4096 characters, no matter what.

Eg. here's a working example. After the first run of the loop, it reads the first 4096 characters, and then waits for more processing each enter, until I end it with an EOF (Ctrl+D). Same thing for fmt.Scan, bufio.NewScanner, bufio ReadLine, ioutil.ReadAll. If I save it to a file, and read it, it works as expected. From stdin it doesn't.

I'm on Arch Linux, 32 bit, Go 1.7, tested in mate-terminal 1.14, tty 8.25, same thing in both of them. And the same thing happens on the hackerrank.com page, I don't know what technology they're using.

Please help!

EDIT:

My input is just a little bit longer than 4096 characters. I checked out the link that Amd shared, and I got the following: my input is only one line containing space separated integers. When I changed the spaces to newlines, it worked. But since the excercise format on hackerrate uses long space-separated lines, the problem is still up, with a refinement.

答案1

得分: 1

我能够解决这个问题,多亏了Ian Lance Taylor:
https://groups.google.com/forum/#!topic/golang-nuts/ndh-1wdsWYs

所以4096个字符是通过N_TTY_BUF_SIZE内核参数限制的我的系统的上限。同样的方法也适用于cat和Python。

无论如何,在hackerrank.com上,我能够用Python解决相同的问题,所以我猜他们对Go的配置有问题,我已经要求他们找出问题所在。

英文:

I was able to solve this thanks to Ian Lance Taylor:
https://groups.google.com/forum/#!topic/golang-nuts/ndh-1wdsWYs

So the 4096 characters is the limit of my system through N_TTY_BUF_SIZE kernel parameter. The same thing worked with cat and Python as well.

Anyway, on hackerrank.com I was able to solve the same exercise in Python, so I guess they have a wrong configuration for Go, I already asked them to find it out.

huangapple
  • 本文由 发表于 2016年8月24日 06:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/39111541.html
匿名

发表评论

匿名网友

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

确定