tool

package module
v0.0.0-...-56972d6 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 44 Imported by: 13

README

tool

介绍

golang 常用工具

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cron = cronT{
	Yearly:   "@yearly",
	Annually: "@annually",
	Monthly:  "@monthly",
	Weekly:   "@weekly",
	Daily:    "@daily",
	Midnight: "@midnight",
	Hourly:   "@hourly",
}
View Source
var FeiShu = &feishu{}
View Source
var String = &strT{}
View Source
var Time = &timeT{}

Functions

func Abs

func Abs[T constraints.Integer | constraints.Float](x T) T

Abs返回x的绝对值。

func ArrayMerge

func ArrayMerge[T any](a ...[]T) []T

ArrayMerge 合并数组到第一个元素

func ArrayPos

func ArrayPos[T comparable](arr []T, item T) (pos int, ok bool)

ArrayPos 获取item在arr中的索引位置

func ArrayRandom

func ArrayRandom[T comparable](s []T) T

ArrayRandom 从切片中随机获取一个值

func ArrayRandom2

func ArrayRandom2[T comparable](s []T) (int, T)

ArrayRandom2 从切片中随机获取一个值

func Bytes2String

func Bytes2String(b []byte) string

Bytes2String 高性能bytes转字符串

func Colorize

func Colorize(s interface{}, c int, disabled bool) string

Colorize 返回用ANSI代码c包装的字符串,disabled为true时为禁用

func CopyTwoSlice

func CopyTwoSlice[T any](s [][]T) (newSlice [][]T)

CopyTwoSlice 复制二维切片

func DeleteSliceItem

func DeleteSliceItem[T comparable](list []T, value T) (newList []T)

DeleteSliceItem 根据值删除切片一个元素 返回新切片

func FormatCaller

func FormatCaller(consoleWriter *zerolog_writer.ConsoleWriter, PathDirLevel int) func(i interface{}) string

func FormatFloat

func FormatFloat(f float64, prec int, FillZero bool) string

FormatFloat 四舍五入格式化小数 f=小数,prec=保留位数,FillZero=小数位数不足是否填充0 true填充 false不填充

func GetParentCaller

func GetParentCaller(dirLevel ...int) string

GetParentCaller 获取父级调用者所在行号

func GetStructTagName

func GetStructTagName(s any, tagName string) (map[string]string, error)

GetStructTagName 获取结构体的 tagName 标签值

func HashCrc32

func HashCrc32(s []byte) uint32

func HashFnv1A

func HashFnv1A(s []byte) uint32

func HttpGet

func HttpGet(url string, header map[string]string) (int, []byte, error)

HttpGet 请求 响应内容过大不建议使用此方法

func HttpPostForm

func HttpPostForm(url string, data url.Values, header map[string]string) (int, []byte, error)

HttpPostForm 请求 响应内容过大不建议使用此方法

func HttpPostJson

func HttpPostJson(url string, bodyRaw string, header map[string]string) (int, []byte, error)

HttpPostJson 请求 响应内容过大不建议使用此方法

func InArray

func InArray[T comparable](arr []T, item T) bool

InArray 判断item是否是arr的一个元素

func IsDigit

func IsDigit(str string) bool

IsDigit 判断一个字符串是否为数字字符串

func JsonCompression

func JsonCompression(jsonByte []byte) ([]byte, error)

JsonCompression json压缩为1行

func MapMerge

func MapMerge[K comparable, V any](m ...map[K]V) map[K]V

MapMerge 合并多个map到一个新map

func MapToStruct

func MapToStruct(m map[string]string, out any) error

MapToStruct 将 map m,转换为 struct, 并赋值到out,StructToMap的反函数

func Max

func Max[T constraints.Ordered](n ...T) T

Max 获取最大值

func Md5

func Md5(str string) string

Md5 返回一个32位md5加密后的字符串

func Min

func Min[T constraints.Ordered](n ...T) T

Min 获取最小值

func Min2

func Min2[T constraints.Ordered](n ...T) (index int, value T)

Min2 获取最小值

func NumericStringSeparate

func NumericStringSeparate(str string, n int, sep string) string

NumericStringSeparate 将字符串str中,连续数字个数超过 n 个的数字字符串,增加分隔符sep 函数介绍 utf8.DecodeRuneInString(arr[i])//输入字符串返回地1个字符的utf8编码 测试字符串:您好,您所查询的防伪码5003515624737308已在2022年08月07日10点46分39秒时间查询过第63次,此防伪码为正品码,若您此次为第一次查询,请谨防假冒! 测试结果:您好,您所查询的防伪码5-0-0-3-5-1-5-6-2-4-7-3-7-3-0-8已在2022年08月07日10点46分39秒时间查询过第63次,此防伪码为正品码,若您此次为第一次查询,请谨防假冒!, want

func PathDirLevelCount

func PathDirLevelCount(pathStr string) int

PathDirLevelCount 获取 pathStr 包含几级目录

func PathExists

func PathExists(path string) (bool, error)

PathExists 判断一个文件或文件夹是否存在 输入文件路径,根据返回的bool值来判断文件或文件夹是否存在

func PathRetainRight

func PathRetainRight(pathStr string, n int) string

PathRetainRight 保留n级目录

func RandInt

func RandInt(min, max int) int

RandInt 获取闭区间的随机值 包含min 不包含max

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 获取闭区间的随机值 包含min 不包含max

func RunFuncName

func RunFuncName() string

RunFuncName 获取正在运行的函数名 包路径/包名.函数名

func RunFuncNameExt

func RunFuncNameExt() string

RunFuncName 获取正在运行的函数名 只包含函数名

func Shuffle

func Shuffle[T any](arr []T)

Shuffle 洗牌算法 主要思路为每次随机挑选一个值,放在数组末尾。然后在n-1个元素的数组中再随机挑选一个值,放在数组末尾,以此类推。 注意,一定要设置随机种子,否则每次返回的值是一样的。

func SliceRepeatItemMost

func SliceRepeatItemMost[T comparable](list []T) (item T, num int)

SliceRepeatItemMost 切片中重复元素最多的元素

func StructToMap

func StructToMap(s any, out map[string]string) error

StructToMap 将 struct s,转换为 map[string]string, 并赋值到out,MapToStruct的反函数

Types

type ConfigToMysqlTargetStruct

type ConfigToMysqlTargetStruct interface {
	Default() any
}

type CryptoCBCAES

type CryptoCBCAES struct {
	// contains filtered or unexported fields
}

AES 加密算法实现 示例

func main() {
	ase, err := NewCryptoAES([]byte("hgfedcba87654321"))
	if err != nil {
		log.Fatalln(err)
	}
	src := []byte("19999999999999999999999中文1111111")
	log.Println("原文", string(src))

	dst := ase.Encrypt(src)
	log.Println("密文", string(dst))

	dst2 := ase.Decrypt(dst)
	log.Println("解密", string(dst2))
}

func NewCryptoCBCAES

func NewCryptoCBCAES(key []byte, paddingType PaddingType) (*CryptoCBCAES, error)

func (*CryptoCBCAES) Decrypt

func (s *CryptoCBCAES) Decrypt(src []byte) []byte

Decrypt 解密

func (*CryptoCBCAES) Encrypt

func (s *CryptoCBCAES) Encrypt(src []byte) []byte

Encrypt 加密

type DebugX

type DebugX struct {
	Host      string
	UrlPath   string
	GoCmdPath string
}

func NewDebugX

func NewDebugX(Host string) *DebugX

func (*DebugX) Allocs

func (s *DebugX) Allocs() (cmd string, stdout, stderr []byte, err error)

AllocsTop 所有内存分配的采样

func (*DebugX) AllocsSvg

func (s *DebugX) AllocsSvg() (cmd string, stdout, stderr []byte, err error)

AllocsSvg 所有内存分配的采样

func (*DebugX) AllocsTop

func (s *DebugX) AllocsTop() (cmd string, stdout, stderr []byte, err error)

AllocsTop 所有内存分配的采样

func (*DebugX) BlockSvg

func (s *DebugX) BlockSvg() (cmd string, stdout, stderr []byte, err error)

BlockSvg block含义是阻塞事件,协程抢锁或者管道的读写阻塞等,都会导致协程的阻塞, 协程阻塞时会记录阻塞时间点cputick,解除阻塞时同样记录时间点cputick,以此计算出协程阻塞时间,同时在解除阻塞时记录调用栈。 mutex含义是锁持有事件,在释放锁时记录时间以及调用栈。

func (*DebugX) BlockTop

func (s *DebugX) BlockTop() (cmd string, stdout, stderr []byte, err error)

func (*DebugX) Command

func (s *DebugX) Command(name string, arg ...string) (cmd string, stdout, stderr []byte, err error)

func (*DebugX) Goroutine

func (s *DebugX) Goroutine() (cmd string, stdout, stderr []byte, err error)

func (*DebugX) GoroutineSvg

func (s *DebugX) GoroutineSvg() (cmd string, stdout, stderr []byte, err error)

GoroutineSvg 所有当前 goroutines 的堆栈跟踪

func (*DebugX) GoroutineTop

func (s *DebugX) GoroutineTop() (cmd string, stdout, stderr []byte, err error)

func (*DebugX) Handle

func (s *DebugX) Handle(ctx *gin.Context)

func (*DebugX) HandleMux

func (s *DebugX) HandleMux(writer http.ResponseWriter, request *http.Request)

func (*DebugX) Heap

func (s *DebugX) Heap() (cmd string, stdout, stderr []byte, err error)

func (*DebugX) HeapSvg

func (s *DebugX) HeapSvg() (cmd string, stdout, stderr []byte, err error)

HeapSvg 查看存活对象的内存分配, 内存的指标可通过heap或者allocs查看,这两个指标采样的数据是一样, 但是heap可以用来采样存活对象的内存分配(采样前可运行GC,这样剩下的都是存活对象了)。 线越粗说明该调用栈内存分配越多

func (*DebugX) HeapTop

func (s *DebugX) HeapTop() (cmd string, stdout, stderr []byte, err error)

func (*DebugX) MutexSvg

func (s *DebugX) MutexSvg() (cmd string, stdout, stderr []byte, err error)

MutexSvg 竞争互斥体持有者的堆栈跟踪 个人理解 Block应该是包含mutex的

func (*DebugX) MutexTop

func (s *DebugX) MutexTop() (cmd string, stdout, stderr []byte, err error)

MutexTop 竞争互斥体持有者的堆栈跟踪

func (*DebugX) Profile

func (s *DebugX) Profile(d time.Duration) (cmd string, stdout, stderr []byte, err error)

func (*DebugX) ProfileSvg

func (s *DebugX) ProfileSvg(d time.Duration) (cmd string, stdout, stderr []byte, err error)

ProfileSvg 分析程序热点,比如发现程序中的死循环。 开启之后,会定时记录当然程序执行点(就是程序正在执行哪一行代码)以及调用栈,采集一段时间之后(默认30秒), 根据这些数据是不是就能分析出来热点代码 线越粗说明该调用cpu耗时越多

func (*DebugX) ProfileTop

func (s *DebugX) ProfileTop(d time.Duration) (cmd string, stdout, stderr []byte, err error)

func (*DebugX) Register

func (s *DebugX) Register(router *gin.Engine)

Register 注册到nginx路由

func (*DebugX) RegisterMux

func (s *DebugX) RegisterMux(router *mux.Router)

Register 注册到nginx路由

func (*DebugX) SetGoCmdPath

func (s *DebugX) SetGoCmdPath(p string) *DebugX

SetGoCmdPath 设置go命令路径

func (*DebugX) SetUrlPath

func (s *DebugX) SetUrlPath(p string) *DebugX

SetUrlPath 修改path

func (*DebugX) Template

func (s *DebugX) Template() *template.Template

Template html模版

type JSON2

type JSON2 struct {
	// contains filtered or unexported fields
}

JSON2 包装一个类型使其直json编解码 可用于 go-redis set一个struct 或 scan

func WithJSON2

func WithJSON2(v any) *JSON2

func (*JSON2) MarshalBinary

func (s *JSON2) MarshalBinary() (data []byte, err error)

MarshalBinary 编码函数

func (*JSON2) UnmarshalBinary

func (s *JSON2) UnmarshalBinary(data []byte) error

UnmarshalBinary 解码函数

type LogRusHook

type LogRusHook struct {
	PathLevel int //输出的目录层级 默认3
}

LogRusHook logrus的内容标准hook

func NewLogRusHook

func NewLogRusHook() LogRusHook

func (LogRusHook) Fire

func (s LogRusHook) Fire(entry *logrus.Entry) error

Fire 重新标准日志输出(代码文件路径和行号,函数名)

func (LogRusHook) Levels

func (s LogRusHook) Levels() []logrus.Level

Levels 日志默认输出等级为所有

type MongoID

type MongoID struct {
	ObjectID primitive.ObjectID
}

func NewMongoID

func NewMongoID() MongoID

NewMongoID 基于mongoID生成更短的唯一字符串

func (MongoID) ToBase62

func (s MongoID) ToBase62() string

ToBase62 转62进制,结果16个字符

func (MongoID) ToRawURLBase64

func (s MongoID) ToRawURLBase64() string

ToRawURLBase64 转 RawURLEncoding,结果16个字符

type MysqlSaveConfig

type MysqlSaveConfig struct {
	Type         string //类型
	TableNotes   string //表注释
	TableName    string //表明
	DB           *gorm.DB
	TargetStruct ConfigToMysqlTargetStruct
}

MysqlSaveConfig 将程序得配置信息存储到mysql

func (*MysqlSaveConfig) Add

func (s *MysqlSaveConfig) Add(defaultVal any) error

Add 将结构体 configToMysql 写入mysql

func (*MysqlSaveConfig) CreateTable

func (s *MysqlSaveConfig) CreateTable() error

CreateTable 创建表

func (*MysqlSaveConfig) Get

func (s *MysqlSaveConfig) Get(rt any) error

Get 获取数据 赋值到结构体rt

func (*MysqlSaveConfig) GetType

func (s *MysqlSaveConfig) GetType() string

func (*MysqlSaveConfig) Set

func (s *MysqlSaveConfig) Set(filedName string, defaultVal any) error

Set 强制更新指定的字段

func (*MysqlSaveConfig) With2

func (*MysqlSaveConfig) WithTableNotes

func (s *MysqlSaveConfig) WithTableNotes(TableNotes string) MysqlSaveConfigWith1

type MysqlSaveConfigNew

type MysqlSaveConfigNew interface {
	With2(Type string, TargetStruct ConfigToMysqlTargetStruct) MysqlSaveConfigWith2
	WithTableNotes(TableNotes string) MysqlSaveConfigWith1
}

func NewMysqlSaveConfig

func NewMysqlSaveConfig(TableName string, DB *gorm.DB) MysqlSaveConfigNew

type MysqlSaveConfigWith1

type MysqlSaveConfigWith1 interface {
	CreateTable() error
}

type MysqlSaveConfigWith2

type MysqlSaveConfigWith2 interface {
	Add(defaultVal any) error
	Get(rt any) error
	GetType() string
	Set(filedName string, defaultVal any) error
}

type PaddingType

type PaddingType int
const (
	PKCS7 PaddingType = 7
	PKCS5 PaddingType = 5
)

type RedisZSetLRU

type RedisZSetLRU struct {
	// contains filtered or unexported fields
}

RedisZSetLRU 基于redis Zset实现 lru缓存 RedisFreshDataCache 最新数据缓存,自动淘汰就数据 使用场景 如:缓存用户最后发送的5条消息,超过5条则自动删除,用户30分钟未发消息则会删除所有消息

func NewRedisZSetLRU

func NewRedisZSetLRU(rdb *redis.Client, RedisKey string) *RedisZSetLRU

func (*RedisZSetLRU) Add

func (s *RedisZSetLRU) Add(Score float64, data interface{}, Expire time.Duration, MaxCacheNumber int64) error

Add 添加 //过期时间 //最大缓存个数

func (*RedisZSetLRU) GetOneByIndex

func (s *RedisZSetLRU) GetOneByIndex(out interface{}, index int64) (Score float64, err error)

GetOneByIndex 根据下标获取一条数据

func (*RedisZSetLRU) GetScoreMax

func (s *RedisZSetLRU) GetScoreMax(out interface{}) (Score float64, err error)

GetScoreMax 获取分值最大的数据

func (*RedisZSetLRU) GetScoreMin

func (s *RedisZSetLRU) GetScoreMin(out interface{}) (Score float64, err error)

GetScoreMin 获取分值最小的数据

Jump to

Keyboard shortcuts

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