https://github.com/schoukri/golist
golist provides useful functions for list operations in Go.
https://github.com/schoukri/golist
go go-generate golang golist list slice
Last synced: about 1 month ago
JSON representation
golist provides useful functions for list operations in Go.
- Host: GitHub
- URL: https://github.com/schoukri/golist
- Owner: schoukri
- License: mit
- Created: 2018-12-04T22:13:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-08T17:31:43.000Z (almost 6 years ago)
- Last Synced: 2024-06-20T14:18:47.085Z (over 1 year ago)
- Topics: go, go-generate, golang, golist, list, slice
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golist
[](https://travis-ci.com/schoukri/golist)
[](./LICENSE)
[](https://godoc.org/github.com/schoukri/golist)
[](https://goreportcard.com/report/github.com/schoukri/golist)
Golist provides useful functions for common list operations in Go.
## Introduction
Working with lists of data in Go using the built-in slice data type and operators can sometimes be a pain. Simple and common operations such as sorting, removing duplicates, and manipulating data can often requires a lot of boilerplate code to accomplish. This package simplifies working with slices by providing easy to use methods for common operations. The methods can also be chained together to enable complex operations on the underlying data with a lot less code, but still highly-readable and easy to maintain.
## Example
Here is an example that starts with a slice of integers, multiplies each item by 2, selects only those items that are greater than 20, removes any duplicates, then finally sorts the integers in ascending order.
```go
s := golist.NewSliceInt(-5, 3, 2, 21, 0, 40, -4, -20, 100, 40, -9, 18, 21, -33, 40)
s.Transform(func(a int) int {
return a * 2
}).Filter(func(a int) bool {
return a > 20
}).Unique().Sort()
for _, value := range s.Data() {
fmt.Println(value)
}
// Output:
// 36
// 42
// 80
// 200
```
## Inspiration
* Go [SliceTricks](https://github.com/golang/go/wiki/SliceTricks) examples and sample code
* C# [Enumerable Class](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable) LINQ Methods
* Mark Phelps' [Optional](https://github.com/markphelps/optional) package for using `go generate` to create custom wrappers for built-in types.
## Warning
Golist is in active development and is not yet ready for use in production. The API is experimental and is subject to change without notice. Suggestions or contributions to improve Golist and help shape its design are welcome and encouraged!