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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改