https://github.com/maxbolgarin/lang
Generic one-liners to work with variables, slices and maps
https://github.com/maxbolgarin/lang
go golang
Last synced: 10 months ago
JSON representation
Generic one-liners to work with variables, slices and maps
- Host: GitHub
- URL: https://github.com/maxbolgarin/lang
- Owner: maxbolgarin
- License: mit
- Created: 2024-09-25T05:12:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-20T19:18:09.000Z (11 months ago)
- Last Synced: 2025-04-20T20:26:21.643Z (11 months ago)
- Topics: go, golang
- Language: Go
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-go - lang - Generic one-liners to work with variables, slices and maps without boilerplate code. (Utilities / Utility/Miscellaneous)
- awesome-go - lang - Generic one-liners to work with variables, slices and maps without boilerplate code. (Utilities / Utility/Miscellaneous)
- awesome-go-with-stars - lang - liners to work with variables, slices and maps without boilerplate code. | 2025-07-15 | (Utilities / Utility/Miscellaneous)
- awesome-go-cn - lang - liners to work with variables, slices and maps without boilerplate code. [![godoc][D]](https://godoc.org/github.com/maxbolgarin/lang) (公用事业公司 / 实用程序/Miscellaneous)
- awesome-go - lang - Generic one-liners to work with variables, slices and maps without boilerplate code. (Utilities / Utility/Miscellaneous)
README
# lang
[![Go Version][version-img]][doc] [![GoDoc][doc-img]][doc] [![Build][ci-img]][ci] [![GoReport][report-img]][report]
**Package `lang` provides useful generic one-liners to work with variables, slices and maps**
```
go get -u github.com/maxbolgarin/lang
```
## Overview
`lang` is a lightweight utility library that adds a functional programming style to Go. It leverages Go's generics to provide type-safe operations for collections. This library saves you from writing boilerplate code for common operations, making your code more readable and maintainable.
## Features
### Slice Operations
- **Transformation**: `Map`, `Convert`, `ConvertWithErr`
- **Filtering**: `Filter`, `WithoutEmpty`, `NotEmpty`
- **Aggregation**: `Reduce`
- **Search**: `FindFirst`, `Contains`, `ContainsFunc`, `IndexOf`, `LastIndexOf`
- **Manipulation**: `Copy`, `SplitByChunkSize`, `Chunk`, `Flatten`, `Reverse`, `Distinct`, `Take`, `Skip`
- **Set Operations**: `Intersect`, `Union`, `Difference`
- **Iteration**: `ForEach`
- **Testing**: `All`, `Any`
- **Advanced**: `Partition`, `Compact`
### Map Operations
- **Creation**: `SliceToMap`, `SliceToMapByKey`, `Mapping`, `ConvertToMap`, `PairsToMap`, `ZipToMap`
- **Transformation**: `ConvertMap`, `ConvertMapWithErr`, `ConvertFromMap`, `ConvertFromMapWithErr`
- **Filtering**: `FilterMap`, `WithoutEmptyKeys`, `WithoutEmptyValues`, `NotEmptyMap`
- **Retrieval**: `Keys`, `KeysIf`, `Values`, `ValuesIf`
- **Manipulation**: `CopyMap`, `MergeMap`
- **Grouping**: `GroupBy`
## Examples
Here are a few examples to get you started:
### Filter even numbers
```go
numbers := []int{1, 2, 3, 4, 5}
evens := lang.Filter(numbers, func(n int) bool {
return n%2 == 0
})
// evens = [2, 4]
```
### Transform a slice
```go
numbers := []int{1, 2, 3}
squares := lang.Convert(numbers, func(n int) int {
return n * n
})
// squares = [1, 4, 9]
```
### Group items by a key
```go
type Person struct {
Name string
Age int
}
people := []Person{
{"Alice", 25},
{"Bob", 30},
{"Charlie", 25},
}
byAge := lang.GroupBy(people, func(p Person) int {
return p.Age
})
// byAge = map[int][]Person{
// 25: [{"Alice", 25}, {"Charlie", 25}],
// 30: [{"Bob", 30}],
// }
```
### Create a map from a slice
```go
users := []string{"user1", "user2", "user3"}
userMap := lang.SliceToMap(users, func(user string) (string, int) {
return user, len(user)
})
// userMap = {"user1": 5, "user2": 5, "user3": 5}
```
### Merge multiple maps
```go
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"b": 3, "c": 4}
merged := lang.MergeMap(map1, map2)
// merged = {"a": 1, "b": 3, "c": 4}
```
## Comparison with Native Go
Without generics, operations like filtering or mapping require writing full implementations each time:
```go
// Without lang
evens := []int{}
for _, n := range numbers {
if n%2 == 0 {
evens = append(evens, n)
}
}
// With lang
evens := lang.Filter(numbers, func(n int) bool {
return n%2 == 0
})
```
Add to your Go program a spice of functional languages: [docs][doc].
You also should try this package: [lo](https://github.com/samber/lo).
[version-img]: https://img.shields.io/badge/Go-%3E%3D%201.19-%23007d9c
[doc-img]: https://pkg.go.dev/badge/github.com/maxbolgarin/lang
[doc]: https://pkg.go.dev/github.com/maxbolgarin/lang
[ci-img]: https://github.com/maxbolgarin/lang/actions/workflows/go.yml/badge.svg
[ci]: https://github.com/maxbolgarin/lang/actions
[report-img]: https://goreportcard.com/badge/github.com/maxbolgarin/lang
[report]: https://goreportcard.com/report/github.com/maxbolgarin/lang