https://github.com/maxbolgarin/abstract
Generic data structures to get rid of boilerplate code in business logic
https://github.com/maxbolgarin/abstract
datastructures generic go golang library
Last synced: 10 months ago
JSON representation
Generic data structures to get rid of boilerplate code in business logic
- Host: GitHub
- URL: https://github.com/maxbolgarin/abstract
- Owner: maxbolgarin
- License: mit
- Created: 2024-11-24T10:54:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-25T12:00:31.000Z (11 months ago)
- Last Synced: 2025-04-25T12:31:32.045Z (11 months ago)
- Topics: datastructures, generic, go, golang, library
- Language: Go
- Homepage:
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - abstract - Abstractions and utilities to get rid of boilerplate code in business logic. (Utilities / Utility/Miscellaneous)
- awesome-go - abstract - Abstractions and utilities to get rid of boilerplate code in business logic. (Utilities / Utility/Miscellaneous)
- awesome-go-cn - abstract
- awesome-go-with-stars - abstract - 10-30 | (Utilities / Utility/Miscellaneous)
- fucking-awesome-go - abstract - Abstractions and utilities to get rid of boilerplate code in business logic. (Utilities / Utility/Miscellaneous)
README
# abstract
[![Go Version][version-img]][doc] [![GoDoc][doc-img]][doc] [![Build][ci-img]][ci] [![GoReport][report-img]][report]
**`abstract` provides a suite of generic data structures to focus on the core business logic and avoid unnecessary boilerplate**
```
go get -u github.com/maxbolgarin/abstract
```
## Overview
`abstract` package provides a suite of generic data structures to enhance code organization, concurrency safety, and utility in Go. The package aims to simplify and abstract the working processes with generic and concurrent data structures such as maps, stacks, sets, and pairs.
### Key Features
- **Generic Data Structures**: Includes Map, Orderer, Stack, Set, and their thread-safe counterparts.
- **Clean Code**: Using of this package allowes you to focus on the core business logic and avoid unnecessary complexity.
- **Concurrency Safety**: Thread-safe structures provided for concurrent access across multiple goroutines.
- **Ease of Use**: Designed to integrate seamlessly and abstract away complex operations into simple APIs.
### Cons
- **Complexity**: Abstraction layers might add complexity for simpler use cases.
- **Performance Overhead**: Concurrency safety through mutexes can add overhead.
- **Learning Curve**: Understanding generics and constraints may require additional learning.
### Data Structures and Safe Alternatives
| Data Structure | Description | Safe Alternative | Key Methods |
|----------------|-------------|------------------|-------------|
| `Map` | Generic map for key-value pairs. | `SafeMap` | Get, Set, Delete, Keys, Values |
| `EntityMap` | Map of entities, providing ordering features. | `SafeEntityMap` | Set, Delete, AllOrdered |
| `Set` | Stores keys uniquely without associated values. | `SafeSet` | Add, Remove, Has |
| `Slice` | Generic slice implementation. | `SafeSlice` | Append, Get, Pop, Delete |
| `Stack` | Generic stack implementation. | `SafeStack` | Push, Pop, Last |
| `UniqueStack` | Stack without duplicates. | `SafeUniqueStack` | Push, Pop, Last |
| `LinkedList` | Generic doubly linked list implementation. | `SafeLinkedList` | Front, Back |
| `OrderedPairs` | Maintains order of key-value pairs. | `SafeOrderedPairs` | Add, Get, Keys, Rand |
## Usage Examples
### Using Map and SafeMap
```go
package main
import (
"fmt"
"github.com/maxbolgarin/abstract"
)
func main() {
// Example using Map
m := abstract.NewMap[string, int]()
m.Set("apple", 5)
fmt.Println(m.Get("apple")) // Output: 5
// Example using SafeMap
sm := abstract.NewSafeMap[string, int]()
sm.Set("banana", 10)
fmt.Println(sm.Get("banana")) // Output: 10
}
```
### Using Stack and SafeStack
```go
package main
import (
"fmt"
"abstract"
)
func main() {
// Example using Stack
s := abstract.NewStack[int]()
s.Push(10)
fmt.Println(s.Pop()) // Output: 10
// Example using SafeStack
ss := abstract.NewSafeStack[int]()
ss.Push(20)
fmt.Println(ss.Pop()) // Output: 20
}
```
### Using EntityMap and SafeEntityMap
```go
package main
import (
"fmt"
"abstract"
)
// Define a simple Entity type
type MyEntity struct {
id string
name string
order int
}
func (e MyEntity) ID() string { return e.id }
func (e MyEntity) Name() string { return e.name }
func (e MyEntity) Order() int { return e.order }
func (e MyEntity) SetOrder(o int) abstract.Entity[string] {
return MyEntity{id: e.id, name: e.name, order: o}
}
func main() {
eMap := abstract.NewEntityMap[string, MyEntity]()
eMap.Set(MyEntity{id: "1", name: "entity1", order: 0})
foundEntity, ok := eMap.LookupByName("entity1")
if ok {
fmt.Println(foundEntity.Name()) // Output: entity1
}
}
```
## Contributions and Issues
Feel free to contribute to this package or report issues you encounter during usage
## License
This project is licensed under the terms of the [MIT License](LICENSE).
[MIT License]: LICENSE.txt
[version-img]: https://img.shields.io/badge/Go-%3E%3D%201.23-%23007d9c
[doc-img]: https://pkg.go.dev/badge/github.com/maxbolgarin/abstract
[doc]: https://pkg.go.dev/github.com/maxbolgarin/abstract
[ci-img]: https://github.com/maxbolgarin/abstract/actions/workflows/go.yaml/badge.svg
[ci]: https://github.com/maxbolgarin/abstract/actions
[report-img]: https://goreportcard.com/badge/github.com/maxbolgarin/abstract
[report]: https://goreportcard.com/report/github.com/maxbolgarin/abstract