Sounds like I can either live with that, or create a separate package.
On Tue, Jan 5, 2016, at 10:53 AM, Brad Fitzpatrick wrote:
Your way allocates with those temporary 4-byte buf buffers.
This way doesn't: http://play.golang.org/p/lBw-Kj7QZ5
or without Grow: http://play.golang.org/p/eUNWnzobKV
On Tue, Jan 5, 2016 at 10:39 AM, Liam Staskawicz wrote:
Thanks. I think these don't provide the functionality I'm looking for,
since (unless I'm mistaken) this still implies encoding the binary data to
an intermediate byte slice before writing to the bytes.Buffer.
i.e., the following does not work
var b bytes.Buffer
// len(b.Bytes()) == 0 here, b.Cap() == 64
binary.BigEndian.PutUint32(b.Bytes(), 0x1234) // fails with index out of
range, since len(b.Bytes()) is 0
even if this worked, we'd still need a way to tell b that we've written
those bytes there (i.e., b.Len() would still return 0 at this point since
we haven't modified the unexported b.off field).
so we're left with doing something like:
buf := make(byte, 4)
which, in addition to being noisier and less direct, involves an extra
copy step (and possibly allocation?).
On Tue, Jan 5, 2016, at 10:26 AM, Brad Fitzpatrick wrote:
.... along with binary.BigEndian, etc.
On Tue, Jan 5, 2016 at 8:20 AM, Liam Staskawicz wrote:
Yes. Buffer.Bytes() is not ideal in this case, since it's not possible to
grow the underlying byte slice when writing to it, or move the read/write
pointer of the buffer.
The idea with this api is that it would support encoding/decoding data
directly to/from the buffer:
var b bytes.Buffer
order := binary.BigEndian
// b.Len() == 4
v, err := b.ReadUint32(order)
// v == 0x1234, b.Len() == 0
On Tuesday, January 5, 2016 at 2:30:14 AM UTC-8, thebroke...@gmail.com
Have you tried using the Buffer.Bytes method
On Tuesday, January 5, 2016 at 2:16:55 AM UTC-8, Liam Staskawicz wrote:
I'm interested in implementing binary ops (i.e., those on
encoding/binary.ByteOrder) on the underlying byte slice in a bytes.Buffer,
such that encoding/decoding binary data to intermediate buffers could be
I haven't found a good way to access the underlying byte slice on
bytes.Buffer, so was considering creating a package that carries a copy of
bytes.Buffer (and maybe bytes.Reader), and adds routines like these:http://pastie.org/10659323
Is there an existing package that provides this functionality, or a better
way to do this?
You received this message because you are subscribed to the Google Groups
To unsubscribe from this group and stop receiving emails from it, send an
email to email@example.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firstname.lastname@example.org.