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 created has a random size from 87 to 90, so: 1 chunk may have 
the size of 87, the next one may have the size of 90, and so on..

I have a similar function in Go which splits the data:

```
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
}
```

The difference between this and the one in python is that the data is 
chunked using a fixed number. Example:

```
for _, chunk := range split(buf[:n], 100) {
    if _, err := out.Write(chunk); err != nil {
    .......
```

This will chunk the data with a fixed size of 100, so every chunk has a 
size of 100, while with python is a random size from 87 to 90.

The desired output is the same thing: the chunks should have a random size 
everytime.

How can I achieve the same goal but in go?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b3b642aa-53a1-41e8-bc90-ee5930a857ccn%40googlegroups.com.

Reply via email to