Steve Crook
Steve Crook

Reputation: 1055

Golang: Slicing and populating byte arrays

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?

Upvotes: 5

Views: 4856

Answers (1)

chendesheng
chendesheng

Reputation: 2129

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

Upvotes: 8

Related Questions