douxun4860 2016-08-23 22:17
浏览 234
已采纳

golang从标准输入中读取长文本

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条回答 默认 最新

  • drmy1050 2016-09-20 09:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配