Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No buffer size limit in bufio.NewReader.ReadString()?

Tags:

go

I have a socket client that read response like this:

message, err := bufio.NewReader(conn).ReadString('\n')

It works perfectly, but the ReadString method seems to have no limit on the buffer size.

Is it possible to add one? I didn't find much in bufio's document.

like image 831
daisy Avatar asked Sep 14 '26 21:09

daisy


1 Answers

For example,

message, err := bufio.NewReaderSize(conn, 1024).ReadString('\n')

To limit the data read, use an io.LimitedReader. For example,

rdr := bufio.NewReader(&io.LimitedReader{R: conn, N: 1024})
message, err := rdr.ReadString('\n')
like image 170
peterSO Avatar answered Sep 17 '26 18:09

peterSO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!