Re: [go-nuts] Go's equivalent of Java's ByteBuffer?

847 views
Skip to first unread message

Dave Cheney

unread,
Feb 16, 2013, 10:57:49 PM2/16/13
to robby.j...@gmail.com, golan...@googlegroups.com
Have you seen this page
http://code.google.com/p/go-wiki/wiki/SliceTricks ? It should be
simple, and lighter weight to use a []byte rather than re implementing
Java's ByteBuffer class.

On Sat, Feb 16, 2013 at 5:50 PM, <robby.j...@gmail.com> wrote:
> Hi all,
>
> I'm in the middle of porting an old Java application that makes an extensive
> amount of use with it's ByteBuffer class. (namely get/put Int/Float/Short).
>
> Is there a package within Go's ecosystem that I can use to port this
> against, or would it be better to wrap ByteBuffer and re-implement each of
> those methods?
>
> Sorry if this seems basic, but if it's buried in a package, I couldn't find
> it.
>
> --
> 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 golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Andrew Wilkins

unread,
Feb 17, 2013, 6:50:05 AM2/17/13
to golan...@googlegroups.com, robby.j...@gmail.com
On Saturday, 16 February 2013 14:50:15 UTC+8, robby.j...@gmail.com wrote:
Hi all,

I'm in the middle of porting an old Java application that makes an extensive amount of use with it's ByteBuffer class. (namely get/put Int/Float/Short). 

Is there a package within Go's ecosystem that I can use to port this against, or would it be better to wrap ByteBuffer and re-implement each of those methods?

To en/decode numbers to/from byte sequences, you should look at encoding/binary (http://golang.org/pkg/encoding/binary/)
There are other encoding packages - just look under "encoding" in the package list.

There's also the bytes package, which contains a type called Buffer: http://golang.org/pkg/bytes/#Buffer.
A *bytes.Buffer is an io.Writer/io.Reader, so it can be used in encoding/binary's Write/Read functions.

Cheers,
Andrew
Reply all
Reply to author
Forward
0 new messages