当前位置: 首页 > 工具软件 > Bigfile-go > 使用案例 >

Golang ReadLine BUFSIZ参数

融伯寅
2023-12-01

先看一个简单的实例:

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    line, isPrefix, err := reader.ReadLine()
    if err != nil {
        fmt.Println("Error reading input:", err)
        return
    }
    if isPrefix {
        fmt.Println("Error: input line too long")
        return
    }
    fmt.Println("Input:", string(line))
}

文档说明如下:

ReadLine tries to return a single line, not including the end-of-line bytes. If the line was too long for the buffer then isPrefix is set and the beginning of the line is returned. The rest of the line will be returned from future calls. isPrefix will be false when returning the last fragment of the line. The returned buffer is only valid until the next call to ReadLine. ReadLine either returns a non-nil line or it returns an error, never both.

如果太长就无法读取,这里太长是超过了BUFSIZ,BUFSIZ可以通过以下代码获取:

#include <stdio.h>

int main() {
        printf("BUFSIZ: %d\n", BUFSIZ);
        return 0;
}

BUFSIZ 一般是8192。

如果超过就会提示"Error: input line too long"

>>> go run main.go < bigfile                                                                                                        ‹git:main ✘› 16:35.03 Sun Apr 09 2023 >>> 
Error: input line too long

参考:https://pkg.go.dev/bufio#Reader.ReadLine

 类似资料: