https://github.com/go-utils/chunk
Obtains an index of specified size length and returns it
https://github.com/go-utils/chunk
Last synced: 8 months ago
JSON representation
Obtains an index of specified size length and returns it
- Host: GitHub
- URL: https://github.com/go-utils/chunk
- Owner: go-utils
- License: mit
- Created: 2022-11-14T02:39:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-14T03:43:38.000Z (over 3 years ago)
- Last Synced: 2024-06-20T12:44:44.009Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# What is this?
Obtains an index of specified size length and returns it.
# Usage
https://go.dev/play/p/vdWueuK-S31
```go
package main
import (
"fmt"
"github.com/go-utils/chunk"
)
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
chunkedArr := make([][]int, 0)
// chunk size is 3
for idx := range chunk.GetIndex(len(arr), 3) {
chunkedArr = append(chunkedArr, arr[idx.From:idx.To])
}
fmt.Printf("%#v\n", chunkedArr)
// Output: [][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}, []int{10}}
chunkedArr = chunk.Chunking(append(arr, 11), 3)
fmt.Printf("%#v\n", chunkedArr)
// Output: [][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}, []int{10, 11}}
}
```
## License
- Under the [MIT License](./LICENSE)
- Copyright (C) 2022 go-utils