When I run the following code
func writeBytes() ([]byte, error) {
var buf bytes.Buffer
dstBytes := bufio.NewWriter(&buf)
writeTonsOfBytes(dstBytes)
b := buf.Bytes()
fmt.Println(len(b))
return b, nil
}
I get the output
32768
Which is signaling to me that there must be a limit on my bytes.Buffer
instance, which I can't find documentation for.
How can I write an unlimited number of bytes to a bytes.Buffer
?
There is no call to
dstBytes.Flush()
in the code. Any bytes that are currently unwritten to buf
will remain unwritten, causing buf
to have an incomplete record of the bytes written to dstBytes
in writeTonsOfBytes
.
It also explains why the number of written bytes would end up as a power of 2 because new bytes are written to the underlying buffer at such an increment.