首页 文章

golang从stdin读取长文本

提问于
浏览
3

我想从os.Stdin读一篇长篇文章,但我无法实现 . 已经阅读了主题中的所有内容,尝试了可行的代码 . 无论如何,每个方法都会减少4096个字符 .

例如 . 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 .

我在Arch Linux,32位,Go 1.7,在mate-terminal 1.14,tty 8.25上测试,两者都是一样的 . 同样的事情发生在hackerrank.com页面上,我不知道他们正在使用什么技术 .

请帮忙!

EDIT:

我的输入只比4096个字符长一点 . 我检查了Amd共享的链接,我得到了以下内容:我的输入只有一行包含空格分隔的整数 . 当我将空格更改为换行符时,它起作用了 . 但由于黑客的练习格式使用长空格分隔线,因此问题仍然存在,并且需要进行改进 .

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配置错误,我已经让他们找到了 .

相关问题