The goal: pick a random number from a list every time a chunk is generated
Python code example to make it more clear.
I have this list:
listbytes = [87, 88, 89, 90]
And this function which splits the data:
def chunks(lst, n):
"Yield successive chunks from lst, where n is a list of possible sizes"
i = 0
while i < len(lst):
k = min(random.choice(n), len(lst) - i)
yield lst[i:i + k]
i += k
And I call it this way:
for chunk in chunks(d, listbytes):
......
Every chunk has a random size between the ints in the listbytes variable, so: 1 chunk may have the size of 87, the next one may have the size of 90, and so on..
Go code:
func split(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
And I call it this way:
for _, chunk := range split(buf[:n], 100) {
.......
How can I achieve the same goal but in go?
Aucun commentaire:
Enregistrer un commentaire