1

I have a message byte Buffer and I would like to append a byte at the end of the Buffer

I tried to append like this:

append(message.Buf, 0xff)
first argument to append must be slice; have *bytes.Buffer

append(0xff,message.Buf)
first argument to append must be slice; have untyped number

How can I make the 0xff as a slice to append?

1
  • 2
    The function append is for appending values to a slice and has absolutely nothing to do with bytes.Buffer which is not a slice. Just Write to your bytes.Buffer. And please read the docs ,especially golang.org/pkg/bytes/#Buffer.WriteByte Commented May 2, 2017 at 12:57

1 Answer 1

5

You have a buffer which is of type bytes.Buffer (or more specifically a pointer to that type). It has a Buffer.WriteByte() method, just use that:

message.Buf.WriteByte(0xff)

The builtin append() function which you tried to call is to append values to slices. bytes.Buffer is not a slice, you can't use that with append() (it is implemented using an internal slice, but that is an implementation detail which you should not build on / utilize).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.