douluoqiu4538
douluoqiu4538
采纳率100%
2017-05-02 12:38

在golang中将字节追加到字节缓冲区

已采纳
我有一个消息字节缓冲区,我想在缓冲区末尾附加一个字节 我试图像这样追加:
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

我应该如何使0xff作为要附加的切片呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsaf415212 dsaf415212 4年前

    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).

    点赞 评论 复制链接分享

相关推荐