bytes: document NewBuffer takes ownership of provided slice #19383
Labels
Documentation
Issues describing a change to documentation.
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/wW5LCvEMT5
What did you expect to see?
inJSON
is unchanged.outJSON
contains the original contents ofinJSON
.What did you see instead?
Contents of
inJSON
have been replaced by gzip binary garbage. Contents ofoutJSON
include the gzip binary header.I think the biggest issue here is that the bytes.NewBuffer documentation does not make it clear that the provided
[]byte
will actually be used for the backing storage of the buffer.This would lead the user to believe that the data will be copied into the buffer as it is with bytes.NewBufferString, when in fact writes to the buffer may alter that underlying data.
My other question is whether it is expected that the gzip writer would not function properly (by overwriting the data with the header before compressing the first bit of data) when reading and writing the same
[]byte
? I suspect that this is a strange edge case that should not be used and is not supported.The text was updated successfully, but these errors were encountered: