modulepackage
0.0.0-20241109021313-6e2457b08dcf
Repository: https://github.com/hjh0924/genericgo.git
Documentation: pkg.go.dev
# README
GenericGo
在 Go 语言早期版本中,由于缺乏泛型,开发者在处理集合类型如切片、列表、队列等时,往往需要编写大量类型特定的代码。这不仅增加了开发工作量,也使得代码难以维护。Go 1.18 的泛型特性解决了这一问题,GenericGo
正是基于这一特性,旨在为 Go 开发者提供一个现代、高效的工具库。
GenericGo
是一个为 Go 语言设计的泛型工具库,它充分利用了 Go 1.18 版本引入的泛型特性,为开发者提供了一套类型安全、灵活且易于使用的编程接口。泛型是编程语言中一种强大的工具,它允许编写更通用、更灵活的代码,减少样板代码,并提高代码的复用性。
目标
-
切片
- 添加
- 删除
- 缩容
- 聚合运算
- 求最大值
- 求最小值
- 求和
- 逆转
- 查找
- 索引
- 映射
- 包含
- 集合运算
- 交集
- 并集
- 差集
- 对称差集
-
Option 模式
- 提供泛型 Option 模式支持
-
Pair
-
随机字符串生成
-
TaskPool
- 按需创建 Goroutine 的并发阻塞的任务池
-
List
- ArrayList
- LinkedList 双向链表
- ConcurrentList 并发安全的 List
- SkipList
-
队列
- 基于 ArrayList
- 基于 LinkedList
- 优先级队列 (基于大根堆)
- 优先级队列 (基于大根堆) (并发安全)
- 延时队列
-
栈
-
堆
-
Map
- 基于 map 的 HashMap 封装
- LinkedMap
-
树
- 红黑树
- 基于红黑树的 TreeMap 和 TreeSet
-
Set
- HashSet
- TreeSet
-
跳表
- 基于跳表的有序 SortedSet
-
并发队列
- 并发队列
- 并发阻塞队列
- 并发阻塞优先级队列
-
统一缓存
- RedisCache
- LRUCache
- PriorityCache
-
限流器
- 基于 Redis + 滑动窗口 实现的限流器
-
gin中间件
- 日志中间件
- IP限流中间件
安装
GenericGo
作为库使用,可以通过 Go Modules 直接引入到您的项目中。使用以下命令:
go get github.com/HJH0924/GenericGo
开发
拉取代码
git clone https://github.com/HJH0924/GenericGo.git
配置环境
make setup
使用方法
以下是如何使用 GenericGo
中的切片添加元素的功能示例,更多示例代码可查看 examples
:
package main
import (
"fmt"
"github.com/HJH0924/GenericGo/slice"
)
func ExampleSliceAdd() {
src := []int{3, 5, 7}
// 使用 GenericGo 的 Add 函数向切片添加元素
src, err := slice.Add[int](src, 0, 1)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(src)
}
func main() {
ExampleSliceAdd()
}
构建和测试
Makefile
提供了多个目标来简化开发流程:
make bench
:运行基准测试。make ut
:执行单元测试。make setup
:执行项目设置脚本。make fmt
:格式化代码。make lint
:运行静态代码分析。make tidy
:整理模块依赖。make check
:执行代码检查流程。
运行 make
命令加上目标名称来执行相应任务。
补充
如果执行
make check
提示找不到goimports
命令,则可能需要添加环境变量执行下面安装
goimports
命令可能会将goimports
安装在你的家目录下go install golang.org/x/tools/cmd/goimports@latest
则需要添加环境变量
export PATH=$PATH:$HOME/go/bin
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Functions
Zero 根据类型参数 T 返回其零值。.
# Type aliases
Comparator 用于比较两个对象的大小 left < right --- -1 left == right --- 0 left > right --- 1.