What is best depends on what you want to achieve. Do you want the most compact encoding? Or the fastest encoding? Or the encoding which is easiest to debug? Or the simplest encoding? Or an encoding which allows you to gracefully add fields to the type as your program evolves without invalidating persisted data?
You state that your type is easily represented as a sequence of ints. But I would ask whether converting to ints is an unnecessary step which will lead to less clear code? Hope this helps.... On Sunday, 23 October 2022 at 08:00:36 UTC+1 mb0 wrote: > hi travis, > > if you can use any encoding you like, you can write your own custom > encoding that uses a varint encoding. the binary encoding interface > implies that the framing or length segmenting of the raw bytes happens > outside that function, so you can just read and write all your ints > using the varint functions from package encoding/binary. > > https://pkg.go.dev/encoding/binary?utm_source=godoc#Varint > > hope that helps > > On 10/22/22 18:12, Travis Keep wrote: > > The type I am writing can be easily encoded as a sequence of ints. When > > I wrote MarshalBinary for my type, I decided to GobEncode this sequence > > of ints to a byte buffer and return the bytes. I learned that > > GobEncoding an empty slice of int takes 18 bytes. So even for small > > instances of my type, I am paying for 18 extra bytes of overhead to > > encode. I chose Gob because it uses variable length encoding for ints, > > so smaller ints require fewer bytes to encode. > > > > Since I know that I will always be encoding and decoding an []int, is > > there a better way to implement MarshalBinary without reimplementing the > > variable length encoding of ints from scratch? > > > > Thank you in advance. > > > > > > -- > > 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 > > <mailto:golang-nuts...@googlegroups.com>. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/golang-nuts/983c6c04-2473-42f1-be29-371e85d66480n%40googlegroups.com > > < > https://groups.google.com/d/msgid/golang-nuts/983c6c04-2473-42f1-be29-371e85d66480n%40googlegroups.com?utm_medium=email&utm_source=footer > >. > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aa4fcda6-9d0c-4f92-9234-93dd3d9f85f2n%40googlegroups.com.