Golang ReadLine BUFSIZ参数

先看一个简单的实例:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值