utils

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

README

go-utils

Go 语言常用工具库集,提供类型转换、Web 响应处理、系统信息、技术指标、消息推送等实用功能。

安装

go get github.com/phrynus/go-utils

模块概览

模块 路径 说明
HTTP响应 res.go Gin统一响应
系统工具 system.go 系统信息、机器码
环境变量 env.go 环境变量加载
Unknown unknown 任意类型智能转换
钉钉 dingtalk 钉钉消息推送
飞书 feishu 飞书消息推送
日志 logger 高性能日志库
技术指标 ta K线技术分析指标
加密 crypto 多种加密、UUID、MD5
U验证平台SDK uyz-u Uverif API客户端

unknown - 任意类型智能转换

  • ✅ 20+ 种类型转换(Bool、Int/Uint/Float 系列、Complex、Duration、Time、String、Bytes、Array、Map、Struct、指针、通道、函数)
  • ✅ 智能类型推断(自动识别 string/int/float/bool/json.Number 等多种输入类型)
  • ✅ 零值安全(所有转换方法均提供 Must 系列,支持默认值参数,nil 值不 panic)
  • ✅ 丰富的时间解析(RFC3339、RFC1123、紧凑格式、时间戳、JSON Date 等 30+ 种格式)
  • ✅ 嵌套 Map 访问(支持 user.profile.name 形式的点号路径)
  • ✅ SmartUnmarshal(JSON 字符串或 Map 自动填充到任意目标结构体,支持多种 key 匹配策略)

👉 详细文档:unknown/README.md


crypto - 加密工具集

  • ✅ 密码哈希(bcrypt)
  • ✅ UUID v7 生成
  • ✅ MD5 签名

👉 详细文档:crypto/README.md


dingtalk - 钉钉消息推送

  • ✅ 文本消息
  • ✅ Markdown消息
  • ✅ 链接消息
  • ✅ ActionCard消息(独立跳转/整体跳转)
  • ✅ FeedCard消息
  • ✅ @用户功能
  • ✅ 签名验证

👉 详细文档:dingtalk/README.md


feishu - 飞书消息推送

  • ✅ 文本消息
  • ✅ 富文本消息(Post)
  • ✅ 图片消息
  • ✅ 分享群名片
  • ✅ 消息卡片(Interactive)
  • ✅ 签名验证
  • ✅ 富文本元素辅助函数

👉 详细文档:feishu/README.md


logger - 高性能日志库

  • ✅ 多种日志级别(INFO、DEBUG、WARN、ERROR)
  • ✅ 日志文件轮转(按大小)
  • ✅ 自动压缩归档文件(gzip)
  • ✅ 控制台彩色输出
  • ✅ 显示文件名和行号
  • ✅ 缓冲区提高性能
  • ✅ 并发安全
  • ✅ Logger克隆和父子关系管理
  • ✅ 主Logger关闭时级联关闭所有子Logger

👉 详细文档:logger/README.md


ta - 技术分析指标库

  • ✅ 20+种技术分析指标
  • ✅ 兼容 go-binance 库K线数据结构
  • ✅ 自动识别多种K线数据格式(结构体/数组)
  • ✅ 高性能并发处理
  • ✅ 支持动态添加K线数据

支持指标:EMA、SMA、MACD、RSI、KDJ、BOLL、ATR、ADX、CCI、OBV、DPO、Stochastic RSI、SuperTrend、T3、VR、Williams %R、RMA、CMF、JingZheMA

👉 详细文档:ta/README.md


crypto - 加密工具集

  • ✅ AES-128/192/256 对称加密(CBC 模式,PKCS7 填充)
  • ✅ DES 对称加密(CBC 模式,PKCS7 填充)
  • ✅ RC4 流加密
  • ✅ RSA 非对称加密(PKCS#1 v1.5,支持自动分块)
  • ✅ bcrypt 密码哈希
  • ✅ MD5 散列
  • ✅ UUID v7 生成(时间有序)
  • ✅ Base64 / Hex 统一编码解码

👉 详细文档:crypto/README.md

uyz-u - U验证平台 API 客户端

  • ✅ 多种加密模式(RSA、AES、DES、RC4、None)
  • ✅ 完整的验签系统
  • ✅ 自动Token管理
  • ✅ 代理配置支持
  • ✅ 链式API调用

功能模块:用户认证、多平台登录(QQ/微信)、用户信息、账号绑定、设备管理、商品和支付、消息管理、云函数

👉 详细文档:uyz-u/README.md


许可证

AGPL-3.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorMessages = map[int]string{
	400: "参数错误",
	401: "未授权",
	403: "禁止访问",
	404: "资源不存在",
	408: "请求超时",
	429: "请求过于频繁",
	500: "服务器内部错误",
	502: "网关错误",
	503: "服务不可用",
	504: "网关超时",
}

ErrorMessages 错误码映射消息

View Source
var Ip string = "0.0.0.0"

Functions

func CheckPort added in v1.3.0

func CheckPort(host string, port int) bool

CheckPort 检查端口是否可用

func DownloadFile added in v1.3.0

func DownloadFile(url, filepath string) error

DownloadFile 下载文件

func E added in v1.5.6

func E(c *gin.Context, code int, msg string)

Error 发送错误响应并设置HTTP状态码

func EnvGet added in v1.5.1

func EnvGet(key string, defaultValue string) string

Get 获取环境变量,如果不存在则返回默认值

func EnvGetBool added in v1.5.1

func EnvGetBool(key string, defaultValue bool) bool

GetBool 获取环境变量,如果不存在则返回默认值

func EnvGetFloat64 added in v1.5.1

func EnvGetFloat64(key string, defaultValue float64) float64

GetFloat64 获取环境变量,如果不存在则返回默认值

func EnvGetInt added in v1.5.1

func EnvGetInt(key string, defaultValue int) int

GetInt 获取环境变量,如果不存在则返回默认值

func EnvLoad added in v1.5.1

func EnvLoad() error

Load 加载配置,从环境变量读取

func EnvSet added in v1.5.1

func EnvSet(key string, value string)

Set 设置环境变量

func EnvUnset added in v1.5.1

func EnvUnset(key string)

Unset 删除环境变量

func GetAvailablePort added in v1.3.0

func GetAvailablePort() int

GetAvailablePort 获取一个可用的端口

func GetBaseboardId added in v1.3.1

func GetBaseboardId() string

获取主板 ID

func GetComputerName added in v1.3.2

func GetComputerName() string

GetComputerName 获取电脑名称/主机名

func GetCpuId added in v1.3.1

func GetCpuId() string

获取CPU ID

func GetLocalIP added in v1.3.0

func GetLocalIP() string

GetLocalIP 获取本机IP地址

func GetMachineCode added in v1.3.1

func GetMachineCode() string

取机器码UUID

func GetMemoryId added in v1.3.1

func GetMemoryId() string

获取内存 ID

func GetOutboundIP added in v1.3.0

func GetOutboundIP() string

GetOutboundIP 获取对外通信的IP地址 如果已缓存有效IP,直接返回;否则尝试多个服务获取IP并缓存结果

func GetProxy added in v1.3.1

func GetProxy() (string, string)

取当前电脑代理

func ResetIPCache added in v1.3.2

func ResetIPCache()

ResetIPCache 重置IP缓存,强制下次调用GetOutboundIP时重新获取

func S added in v1.5.6

func S(c *gin.Context, data interface{}, msg string)

Success 发送成功响应并设置HTTP状态码

func SuccessFile added in v1.6.0

func SuccessFile(c *gin.Context, filename string, data []byte, contentType string)

SuccessFile 发送文件下载响应

func SuccessHTML added in v1.6.0

func SuccessHTML(c *gin.Context, html string)

SuccessHTML 发送 HTML 响应

Types

type ResData added in v1.5.6

type ResData[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data,omitempty"`
	Time int64  `json:"time"`
	Sign string `json:"sign,omitempty"` // 响应签名
}

ResData 统一响应格式

type SystemInfo added in v1.3.0

type SystemInfo struct {
	OS           string `json:"os"`
	Arch         string `json:"arch"`
	CPUCores     int    `json:"cpu_cores"`
	GoVersion    string `json:"go_version"`
	Hostname     string `json:"hostname"`
	TempDir      string `json:"temp_dir"`
	HomeDir      string `json:"home_dir"`
	WorkingDir   string `json:"working_dir"`
	LocalIP      string `json:"local_ip"`
	OutboundIP   string `json:"outbound_ip"`
	ComputerName string `json:"computer_name"`
	CPUId        string `json:"cpu_id"`
	BaseboardId  string `json:"baseboard_id"`
	MemoryId     string `json:"memory_id"`
	MachineCode  string `json:"machine_code"`
	HttpProxy    string `json:"http_proxy"`
	HttpsProxy   string `json:"https_proxy"`
}

SystemInfo 系统信息

func GetSystemInfo added in v1.3.0

func GetSystemInfo() (*SystemInfo, error)

GetSystemInfo 获取系统信息

type ToolMap added in v1.6.0

type ToolMap struct {
	AppKey     string `json:"app_key"`  // 应用密钥
	Compress   bool   `json:"compress"` // 是否启用压缩
	Encryption *struct {
		Key string `json:"key"` // 加密密钥 aes
		IV  string `json:"iv"`  // 加密向量 aes
	} `json:"encryption"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL