Slice chunking in Go

You don’t need to make new slices, just append slices of logs to the divided slice. http://play.golang.org/p/vyihJZlDVy var divided [][]string chunkSize := (len(logs) + numCPU – 1) / numCPU for i := 0; i < len(logs); i += chunkSize { end := i + chunkSize if end > len(logs) { end = len(logs) } divided … Read more

How slicing in Python works

The syntax is: a[start:stop] # items start through stop-1 a[start:] # items start through the rest of the array a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole array There is also the step value, which can be used with any of the above: a[start:stop:step] # start through not … Read more

How to inspect slice header?

The slice header is represented by the reflect.SliceHeader type: type SliceHeader struct { Data uintptr Len int Cap int } You may use package unsafe to convert a slice pointer to *reflect.SliceHeader like this: sh := (*reflect.SliceHeader)(unsafe.Pointer(&newSlice2)) And then you may print it like any other structs: fmt.Printf(“%+v”, sh) Output will be (try it on … Read more

Big O of append in Golang

This all depends on the actual implementation used, but I’m basing this on the standard Go as well as gccgo. Slices Reslicing means changing an integer in a struct (a slice is a struct with three fields: length, capacity and pointer to backing memory). If the slice does not have sufficient capacity, append will need … Read more