Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mokiat/gog
A Go library with useful generic functions and types
https://github.com/mokiat/gog
generics go golang library
Last synced: 6 days ago
JSON representation
A Go library with useful generic functions and types
- Host: GitHub
- URL: https://github.com/mokiat/gog
- Owner: mokiat
- License: mit
- Created: 2022-10-28T07:21:25.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-05T22:03:32.000Z (9 days ago)
- Last Synced: 2024-11-05T22:34:28.389Z (9 days ago)
- Topics: generics, go, golang, library
- Language: Go
- Homepage: https://pkg.go.dev/github.com/mokiat/gog
- Size: 69.3 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoG (Go Generics)
[![Go Reference](https://pkg.go.dev/badge/github.com/mokiat/gog.svg)](https://pkg.go.dev/github.com/mokiat/gog)
[![Go Report Card](https://goreportcard.com/badge/github.com/mokiat/gog)](https://goreportcard.com/report/github.com/mokiat/gog)GoG is a Go library with useful generic functions and types.
Since the introduction of generics in Go 1.18, a number of useful and reusable
data structures, algorithms and utility functions are now possible. This library
attempts to cover some of the most common use cases.It avoids duplicating functions that are already provided by
[slices](https://pkg.go.dev/slices) and
[maps](https://pkg.go.dev/maps).For a complete list on available functions and types, check the
godoc documentation for this project:- [gog](https://pkg.go.dev/github.com/mokiat/gog) - general utility functions
- [gog/ds](https://pkg.go.dev/github.com/mokiat/gog/ds) - data structures
- [gog/filter](https://pkg.go.dev/github.com/mokiat/gog) - data filtering
- [gog/opt](https://pkg.go.dev/github.com/mokiat/gog) - optional fields and types## Examples
**Converting slice values from one type to another:**
```go
source := []int{1, 2, 3, 4}
target := gog.Map(source, func(item int) float64 {
return float64(item) / 2.0
})
// target = []float64{0.5, 1.0, 1.5, 2.0}
```**Removing duplicate items in a slice:**
```go
source := []string{"john", "bill", "eric", "john", "max", "eric"}
target := gog.Dedupe(source)
// target = []string{"john", "bill", "eric", "max"}
```**Finding an item in a slice:**
```go
source := []string{"user 01", "user 02", "user 05", "user 33"}
foundItem, ok := gog.FindFunc(source, func(item string) bool {
return strings.Contains(item, "02")
})
// ok = true
// foundItem = "user 02"
```**Selecting specific slice items:**
```go
source := []int{1, 2, 3, 4}
target := gog.Select(source, func(item int) bool {
return item % 2 == 0
})
// target = []int{2, 4}
```