Home >Backend Development >Golang >How to Efficiently Chunk a Slice in Go?
Slice Chunking in Go
As you have encountered, chunking a slice in Go can be achieved through the following steps:
Here's an updated example based on your code:
var divided [][]string numCPU := runtime.NumCPU() chunkSize := (len(logs) + numCPU - 1) / numCPU for i := 0; i < len(logs); i += chunkSize { end := i + chunkSize if end > len(logs) { end = len(logs) } temp := logs[i:end] // Create a new slice for the chunk divided = append(divided, temp) // Append the chunk to the divided slice }
The above is the detailed content of How to Efficiently Chunk a Slice in Go?. For more information, please follow other related articles on the PHP Chinese website!