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

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

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

报告相同问题?

悬赏问题

  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader