Golang:切片和填充字节数组 [英] Golang: Slicing and populating byte arrays

查看:1198
本文介绍了Golang:切片和填充字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写使用golang数据包协议。作为协议将有一个固定的长度,它似乎是一个很好的起点,分配的内存的确切数额。例如。

I'm trying to write a packet protocol using golang. As the protocol will have a fixed length, it seems like a good starting point to allocate the exact amount of memory. E.g.

packet := make([]byte, 1024)

我不明白的是,如何在填充该数据包的具体内容。我想说的是这样的: -

What I don't understand is how to then populate specific elements of that packet. I want to say something like:-

slice = pointer(packet[512])
slice = []byte("abcdef")

该结果是该分组[512:518] == []字节(ABCDEF)。我对数组和切片阅读文档说明如何修改在片单字节而不是连续的字节序列。有没有做到这一点的方法?

The result being that packet[512:518] == []byte("abcdef"). The docs I've read on Arrays and Slices show how to modify a single byte in a slice but not a contiguous sequence of bytes. Is there a method to do this?

推荐答案

您不能做到这一点。我可以告诉最接近的方式是使用副本。检查: http://play.golang.org/p/PtGJuVgEjc

You can’t do this. The closest way I can tell is use copy. check: http://play.golang.org/p/PtGJuVgEjc

这篇关于Golang:切片和填充字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆