dongrui6787 2015-12-09 21:33
浏览 115
已采纳

开始-如何增加最大Stdin输入长度?

What's the recommended way to allow more than 1024 characters when reading from standard input in Go?

For example, this code using bufio.Scanner has a maximum input length of 1024.

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    input := scanner.Text()
    fmt.Println(input)
}

Update after some suggested answers... Sorry guys - I must still be doing something wrong, or misstated the question. I tried both suggestions and still see the same issue. Below is an updated version of the code. The symptom is that the scanner won't accept input after the 1024th character. e.g. Try to run this then paste in a string that's 1025 characters long, and it'll stop accepting input after character 1024.

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "log"
    "os"
)

func main() {
    var buffer bytes.Buffer
    scanner := bufio.NewScanner(os.Stdin)

    for {
        done := scanner.Scan()
        buffer.WriteString(scanner.Text())
        if done == true {
            if scanner.Err() != nil {
                log.Fatal("Error scanning input")
            }
            break
        }
    }
    fmt.Println(buffer.String())
}
  • 写回答

3条回答 默认 最新

  • dongtangjie0495 2015-12-10 04:29
    关注

    The main problem with this code is that the return value of scanner.Scan is not true if the scanner is done, but true if the scanner is not done.

    While we're fixing that, let's tidy up another thing, since you don't need to use

    if done == true
    

    but can simply do

    if done
    

    Also, remember that done = Scanner.Text(), and serves as the terminating condition, and we can tidy up the code really nicely with:

    for scanner.Scan() { // This does what you want, looping until scanner
                         // is no longer true.
        buffer.WriteString(scanner.Text())
    }
    if err := scanner.Err(); err != nil {           // Let's hold on to err...
        log.Fatalf("Error scanning input: %s", err) // and use it here.
    }
    

    You get to discard a lot of lines, and you only check Err() once at the end of the loop. No need to use break.

    Hope that works for you, and send me a line if you need something else.

    Edit: Two more things to be aware of.

    1) bufio.Scanner.Scan() will be eating all your separator tokens. Maybe that's okay, or maybe you want to keep adding them back in.

    2) bufio.Scanner.Scan() may panic if your SplitFunc is wrong, or someone is crafting poor input for your SplitFunc, and bytes.Buffer.WriteString() may panic if the buffer gets too large. If you're using these in a production system, make sure that you handle these panics in whatever way makes sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?