douluoqiu4538
2017-05-02 12:38
采纳率: 100%
浏览 1.9k
已采纳

在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作为要附加的切片呢?

图片转代码服务由CSDN问答提供 功能建议

我有一个消息字节缓冲区,我想在缓冲区末尾附加一个字节

我试图像这样追加:

  append(message.Buf,0xff)
要追加的第一个参数必须是slice; 具有* bytes.Buffer 
 
append(0xff,message.Buf)
要附加的第一个参数必须是slice; 有未键入的数字
   
 
 

如何将0xff作为要附加的切片?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsaf415212 2017-05-02 12:56
    已采纳

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

    打赏 评论

相关推荐 更多相似问题