Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidroman0o/multigroup
Simple Go utility for dynamic multi-level grouping (multi group) of slice elements based on custom key selectors
https://github.com/davidroman0o/multigroup
data-grouping dynamic-groups group group-by groupby multi multigroup
Last synced: about 11 hours ago
JSON representation
Simple Go utility for dynamic multi-level grouping (multi group) of slice elements based on custom key selectors
- Host: GitHub
- URL: https://github.com/davidroman0o/multigroup
- Owner: davidroman0O
- License: mit
- Created: 2023-11-09T02:53:57.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-11-09T02:57:14.000Z (12 months ago)
- Last Synced: 2024-01-07T09:16:13.023Z (10 months ago)
- Topics: data-grouping, dynamic-groups, group, group-by, groupby, multi, multigroup
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# MultiGroup
The `multigroup` Go module provides a utility function for grouping slices of any type based on multiple key selectors. It's particularly useful when you need to categorize data into nested groups dynamically.
## Installation
```sh
go get github.com/davidroman0O/multigroup
```## Usage
To use the `MultiGroup` function, you need to provide a slice of elements and a set of key selectors. Each selector function should take an element as input and return a key name and its corresponding value as a string.
Here is a simple example of how to use `MultiGroup`:
```go
package mainimport (
"fmt"
"strconv"
"github.com/davidroman0O/multigroup"
)// Define your data structure.
type Person struct {
Name string
Country string
Age int
}func main() {
// Prepare your data.
people := []Person{
{Name: "Alice", Country: "USA", Age: 30},
{Name: "Bob", Country: "USA", Age: 30},
{Name: "Charlie", Country: "USA", Age: 25},
{Name: "Diana", Country: "UK", Age: 30},
}// Define key selectors.
countrySelector := func(p Person) (string, string) { return "Country", p.Country }
ageSelector := func(p Person) (string, string) { return "Age", strconv.Itoa(p.Age) }// Group the data.
groupedPeople := multigroup.By(people, countrySelector, ageSelector)// Output the results.
for _, group := range groupedPeople {
fmt.Printf("Group: %+v\n", group.Keys)
for _, person := range group.Items {
fmt.Printf(" %+v\n", person)
}
}
}
```## License
`multigroup` is open-source software licensed under the MIT license. See the `LICENSE` file for more details.