Golang: Slicing and populating byte arrays

旧街凉风 提交于 2019-12-23 10:47:12

问题


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

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?


回答1:


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



来源:https://stackoverflow.com/questions/24806867/golang-slicing-and-populating-byte-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!