问题描述
我想写使用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屋!