goutils

package module
v1.0.39 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 39 Imported by: 46

README

# goutils

Build Status Go Report Card Codacy Badge

Golang 通用类函数工具包

在线文档:https://godoc.org/github.com/axiaoxin-com/goutils

功能概览

Redis 相关

文件相关

单元测试相关

IP 相关

时间相关

slice 相关

string 相关

配置相关

URL 相关

Struct 相关

HTTP 请求相关

时间相关

统计

Map相关

随机

其他分类

Documentation

Overview

Package goutils 是一个将开发中常用的通用类函数封装统一存放在这里以便复用的函数工具包

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGoCacheKeyNotFound go-cache get key 不存在
	ErrGoCacheKeyNotFound = errors.New("cache key not found")
	// ErrGoCacheSetResultFailed go-cache get 转换为结果对象失败
	ErrGoCacheSetResultFailed = errors.New("cache set result failed")
)
View Source
var ChinaHolidays = map[string]map[string]bool{
	"2021": ChinaHolidays2021,
	"2022": ChinaHolidays2022,
	"2023": ChinaHolidays2023,
	"2024": ChinaHolidays2024,
}

ChinaHolidays 中国法定节假日

View Source
var ChinaHolidays2021 = map[string]bool{

	"2021-01-01": true,
	"2021-01-02": true,
	"2021-01-03": true,

	"2021-02-11": true,
	"2021-02-12": true,
	"2021-02-13": true,
	"2021-02-14": true,
	"2021-02-15": true,
	"2021-02-16": true,
	"2021-02-17": true,

	"2021-04-03": true,
	"2021-04-04": true,
	"2021-04-05": true,

	"2021-05-03": true,
	"2021-05-04": true,
	"2021-05-05": true,

	"2021-06-12": true,
	"2021-06-13": true,
	"2021-06-14": true,

	"2021-09-19": true,
	"2021-09-20": true,
	"2021-09-21": true,

	"2021-10-01": true,
	"2021-10-02": true,
	"2021-10-03": true,
	"2021-10-04": true,
	"2021-10-05": true,
	"2021-10-06": true,
	"2021-10-07": true,
}

ChinaHolidays2021 2021年中国法定节假日

View Source
var ChinaHolidays2022 = map[string]bool{

	"2022-01-01": true,
	"2022-01-02": true,
	"2022-01-03": true,

	"2022-01-31": true,
	"2022-02-01": true,
	"2022-02-02": true,
	"2022-02-03": true,
	"2022-02-04": true,
	"2022-02-05": true,
	"2022-02-06": true,

	"2022-04-03": true,
	"2022-04-04": true,
	"2022-04-05": true,

	"2022-04-30": true,
	"2022-05-01": true,
	"2022-05-02": true,
	"2022-05-03": true,
	"2022-05-04": true,

	"2022-06-03": true,
	"2022-06-04": true,
	"2022-06-05": true,

	"2022-09-10": true,
	"2022-09-11": true,
	"2022-09-12": true,

	"2022-10-01": true,
	"2022-10-02": true,
	"2022-10-03": true,
	"2022-10-04": true,
	"2022-10-05": true,
	"2022-10-06": true,
	"2022-10-07": true,

	"2022-12-30": true,
	"2022-12-31": true,
}

ChinaHolidays2022 2022年中国法定节假日

View Source
var ChinaHolidays2023 = map[string]bool{

	"2023-01-01": true,

	"2023-01-22": true,
	"2023-01-23": true,
	"2023-01-24": true,
	"2023-01-25": true,
	"2023-01-26": true,
	"2023-01-27": true,
	"2023-01-28": true,

	"2023-04-03": true,
	"2023-04-04": true,
	"2023-04-05": true,

	"2023-04-29": true,
	"2023-04-30": true,
	"2023-05-01": true,

	"2023-06-22": true,
	"2023-06-23": true,
	"2023-06-24": true,

	"2023-09-29": true,
	"2023-09-30": true,

	"2023-10-01": true,
	"2023-10-02": true,
	"2023-10-03": true,
	"2023-10-04": true,
	"2023-10-05": true,
	"2023-10-06": true,
}

ChinaHolidays2023 2023年中国法定节假日

View Source
var ChinaHolidays2024 = map[string]bool{

	"2024-01-01": true,

	"2024-02-10": true,
	"2024-02-11": true,
	"2024-02-12": true,
	"2024-02-13": true,
	"2024-02-14": true,
	"2024-02-15": true,
	"2024-02-16": true,
	"2024-02-17": true,

	"2024-04-04": true,
	"2024-04-05": true,
	"2024-04-06": true,

	"2024-05-01": true,
	"2024-05-02": true,
	"2024-05-03": true,
	"2024-05-04": true,
	"2024-05-05": true,

	"2024-06-10": true,

	"2024-09-15": true,
	"2024-09-16": true,
	"2024-09-17": true,

	"2024-10-01": true,
	"2024-10-02": true,
	"2024-10-03": true,
	"2024-10-04": true,
	"2024-10-05": true,
	"2024-10-06": true,
	"2024-10-07": true,
}

ChinaHolidays2024 2024年中国法定节假日 https://www.gov.cn/zhengce/content/202310/content_6911527.htm

View Source
var (
	JSONTimeFormat = "2006-01-02 15:04:05"
)

JSONTimeFormat 定义 JSONTime 的时间格式 该值可以被外部修改为指定的其他格式

View Source
var RedisClusterInstances sync.Map

RedisClusterInstances 按 which key 保存 redis cluster 客户端实例

View Source
var RedisInstances sync.Map

RedisInstances 按 which key 保存 redis 客户端实例

View Source
var RedisSentinelInstances sync.Map

RedisSentinelInstances 按 which key 保存 redis sentinel 客户端实例

View Source
var ValidatorTagName = "binding"

ValidatorTagName 结构体 validator 的 tag 名

Functions

func AnonymousName added in v1.0.24

func AnonymousName(ctx context.Context, serviceid int, ip, ua string, prefixs ...string) string

AnonymousName 返回游客昵称

func AvgFloat64 added in v1.0.5

func AvgFloat64(f []float64) (float64, error)

AvgFloat64 平均值

func CheckPasswordHash added in v1.0.29

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash 密码校验

func ChunkFloat64Slice added in v1.0.3

func ChunkFloat64Slice(data []float64, chunkSize int) [][]float64

ChunkFloat64Slice float64 slice 按指定大小进行切块

func CloseRedisInstances

func CloseRedisInstances()

CloseRedisInstances 关闭全部的 redis 连接并重置 RedisInstances

func CopyFile

func CopyFile(sourceFile, destinationFile string) (err error)

CopyFile 复制文件

func GetGoCache added in v1.0.3

func GetGoCache(c *cache.Cache, key string, resultPointer interface{}) error

GetGoCache 封装go-cache 的 Get 方法支持直接获取具体类型

func GetLatestTradingDay added in v1.0.21

func GetLatestTradingDay() string

GetLatestTradingDay 返回最近一个交易日string类型日期:YYYY-mm-dd

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP 获取当前 IP

func HTTPGET added in v1.0.3

func HTTPGET(ctx context.Context, cli *http.Client, apiurl string, header map[string]string, rspPointer interface{}) error

HTTPGET 发送 http get 请求并将返回结果 json unmarshal 到 rspPointer

func HTTPGETRaw added in v1.0.12

func HTTPGETRaw(ctx context.Context, cli *http.Client, apiurl string, header map[string]string) ([]byte, error)

HTTPGETRaw 发送 http get 请求

func HTTPPOST added in v1.0.3

func HTTPPOST(ctx context.Context, cli *http.Client, req *http.Request, rspPointer interface{}) error

HTTPPOST 发送 http post 请求并将结果 json unmarshal 到 rspPointer

func HTTPPOSTRaw added in v1.0.13

func HTTPPOSTRaw(ctx context.Context, cli *http.Client, req *http.Request) ([]byte, error)

HTTPPOSTRaw 发送 http post 请求

func HashPassword added in v1.0.29

func HashPassword(password string) (string, error)

HashPassword 密码加密

func InitViper

func InitViper(configFile string, onConfigChangeRun func(fsnotify.Event)) error

InitViper 根据配置文件路径和名称初始化 viper 并监听变化 configFile 配置文件 onConfigChangeRun 配置文件发生变化时的回调函数

func IntHashDecode added in v1.0.34

func IntHashDecode(s string, salt string, minLength int, prefix string) int

IntHashDecode 返回hash值对应的int

func IntHashEncode added in v1.0.34

func IntHashEncode(i int, salt string, minLength int, prefix string) string

IntHashEncode 返回int的hash值

func IntsMinMax added in v1.0.35

func IntsMinMax(ints ...int) (min int, max int)

IntsMinMax 返回给定ints中的最小值和最大值

func IsEmailValid added in v1.0.25

func IsEmailValid(email string) bool

IsEmailValid 验证邮箱地址是否合法

func IsEqualStringSlice

func IsEqualStringSlice(s1, s2 []string) bool

IsEqualStringSlice 判断两个 string slice 是否相同

func IsInitedViper

func IsInitedViper() bool

IsInitedViper 返回 viper 是否已初始化

func IsIntInSlice added in v1.0.3

func IsIntInSlice(i int, s []int) bool

IsIntInSlice 判断字符串是否在给定的字符串列表中

func IsStrInSlice added in v1.0.3

func IsStrInSlice(i string, s []string) bool

IsStrInSlice 判断字符串是否在给定的字符串列表中

func IsTradingDay added in v1.0.22

func IsTradingDay() bool

IsTradingDay 返回当期是否为交易日

func JaccardSimilarity added in v1.0.19

func JaccardSimilarity(s1, s2 []interface{}) float64

JaccardSimilarity Jaccard similarity相似性系数 J(A, B) = |A ∩ B | / | A ∪ B | A,B分别代表符合某种条件的集合:两个集合交集的大小/两个集合并集的大小,交集=并集意味着2个集合完全重合。

func MD5 added in v1.0.35

func MD5(v string) string

MD5 string md5

func MapIntIntSortedKeys added in v1.0.36

func MapIntIntSortedKeys(m map[int]int, desc bool) []int

MapIntIntSortedKeys 返回map[int]int{}按value排序的keys

func MapStrIntSortedKeys added in v1.0.36

func MapStrIntSortedKeys(m map[string]int, desc bool) []string

MapStrIntSortedKeys 返回map[string]int{}按value排序的keys

func MidValueFloat64 added in v1.0.14

func MidValueFloat64(values []float64) (float64, error)

MidValueFloat64 获取中位数

func NewHTTPGetURLWithQueryString added in v1.0.3

func NewHTTPGetURLWithQueryString(ctx context.Context, apiurl string, params map[string]string) (string, error)

NewHTTPGetURLWithQueryString 创建带 querystring 的 http get 请求 url

func NewHTTPJSONReq added in v1.0.4

func NewHTTPJSONReq(ctx context.Context, apiurl string, reqData interface{}) (*http.Request, error)

NewHTTPJSONReq 根据参数创建 json 请求

func NewHTTPMultipartReq added in v1.0.3

func NewHTTPMultipartReq(ctx context.Context, apiurl string, reqData map[string]string) (*http.Request, error)

NewHTTPMultipartReq 根据参数创建 form-data 请求

func NewRedisClient

func NewRedisClient(opt *redis.Options) (*redis.Client, error)

NewRedisClient 返回 redis 的客户端连接对象

func NewRedisClusterClient

func NewRedisClusterClient(opt *redis.ClusterOptions) (*redis.ClusterClient, error)

NewRedisClusterClient 返回 redis cluster 的连接对象

func NewRedisFailoverClient

func NewRedisFailoverClient(opt *redis.FailoverOptions) (*redis.Client, error)

NewRedisFailoverClient 返回带 sentinel 的 redis 连接对象

func RandInt added in v1.0.37

func RandInt(start, end int) int

RandInt 返回一个范围为 [start, end] 的int型随机数

func RedisClient

func RedisClient(which string) (*redis.Client, error)

RedisClient 根据 viper 配置返回 redis 客户端

func RedisClusterClient

func RedisClusterClient(which string) (*redis.ClusterClient, error)

RedisClusterClient 根据 viper 配置返回 redis cluster 实例

func RedisSentinelClient

func RedisSentinelClient(which string) (*redis.Client, error)

RedisSentinelClient 根据 viper 配置返回 redis 客户端

func RemoveAllWhitespace

func RemoveAllWhitespace(s string) string

RemoveAllWhitespace 删除字符串中所有的空白符

func RemoveDuplicateWhitespace added in v1.0.3

func RemoveDuplicateWhitespace(s string, trim bool) string

RemoveDuplicateWhitespace 删除字符串中重复的空白字符为单个空白字符 trim: 是否去掉首位空白

func RemoveMarkdownTags added in v1.0.38

func RemoveMarkdownTags(markdownText string) string

RemoveMarkdownTags 移除markdown文本中的标记返回纯文本内容

func RemoveStringSliceItemByIndex

func RemoveStringSliceItemByIndex(slice []string, index int) []string

RemoveStringSliceItemByIndex 根据下标删除 string slice 中的元素

func RequestHTTPHandler

func RequestHTTPHandler(app http.Handler, method, path string, body []byte, header map[string]string) (*httptest.ResponseRecorder, error)

RequestHTTPHandler 根据参数请求传入的 http.Handler 对应的 path 接口,用于接口测试 返回 ResponseRecorder: https://golang.org/pkg/net/http/httptest/#ResponseRecorder

func RequestHTTPHandlerFunc

func RequestHTTPHandlerFunc(f http.HandlerFunc, method string, body []byte, header map[string]string) ([]byte, error)

RequestHTTPHandlerFunc 根据参数请求传入的 http.HandlerFunc 返回请求处理结果 body ,用于接口测试

func ReverseFloat64Slice added in v1.0.19

func ReverseFloat64Slice(numbers []float64)

ReverseFloat64Slice 直接反转 float64 列表,无返回值

func ReverseString

func ReverseString(s string) string

ReverseString 翻转字符串

func ReversedFloat64Slice added in v1.0.19

func ReversedFloat64Slice(numbers []float64) []float64

ReversedFloat64Slice 反转 float64 列表,有返回值

func SetRedisInstances added in v1.0.3

func SetRedisInstances(which string, rdb *redis.Client) error

SetRedisInstances 设置 redis 对象到 RedisInstances 中

func SplitStringFields added in v1.0.20

func SplitStringFields(s string) []string

SplitStringFields 将传入字符串分割为slice

func StdDeviationFloat64 added in v1.0.6

func StdDeviationFloat64(fs []float64) (float64, error)

StdDeviationFloat64 求标准差

func StrSimilarity added in v1.0.11

func StrSimilarity(s1, s2 string, algorithm string) float64

StrSimilarity 提供不同的算法检测文本相似度

func StrToTime added in v1.0.2

func StrToTime(layout, value string, loc ...*time.Location) (time.Time, error)

StrToTime 字符串时间转 time 对象

func StructTagList added in v1.0.9

func StructTagList(item interface{}, tag string) []string

StructTagList 获取结构体 tag 列表

func StructToMap added in v1.0.9

func StructToMap(item interface{}, tag string) map[string]interface{}

StructToMap 结构体转 Map

func StructToURLValues

func StructToURLValues(item interface{}, tag string) (values url.Values)

StructToURLValues 将结构体指针对象转换为 url.Values , key 为 json tag , value 为结构体字段值,没有 json tag 则使用字段名称

func URLKey

func URLKey(prefix, u string) string

URLKey 根据 url 生成 key ,默认使用 url escape ,长度超过 200 则使用 sha1 结果

func UnixTimestampTrim added in v1.0.2

func UnixTimestampTrim(ts int64, sec int64) int64

UnixTimestampTrim 将 Unix 时间戳(秒)以指定秒数进行规整

func ValidationErrorToText added in v1.0.2

func ValidationErrorToText(e validator.FieldError) string

ValidationErrorToText error msg for human

func VarianceFloat64 added in v1.0.6

func VarianceFloat64(fs []float64) (float64, error)

VarianceFloat64 求方差

func YiWanString added in v1.0.15

func YiWanString(num float64) string

YiWanString 将数字转换为 亿/万

Types

type ErrCode

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

ErrCode 错误码结构体

func NewErrCode

func NewErrCode(code interface{}, msg string, errs ...error) *ErrCode

NewErrCode 创建错误码,code 可以是任意类型

func (*ErrCode) AppendError

func (c *ErrCode) AppendError(errs ...error) *ErrCode

AppendError 添加 err 到 errs 中

func (*ErrCode) AppendMsg added in v1.0.1

func (c *ErrCode) AppendMsg(msg string) *ErrCode

AppendMsg 追加 msg 字段

func (*ErrCode) Code

func (c *ErrCode) Code() interface{}

Code 返回 code

func (*ErrCode) Decode

func (c *ErrCode) Decode() (interface{}, string, []error)

Decode 返回结构体里面的字段

func (*ErrCode) Error

func (c *ErrCode) Error() string

Error 拼接所有 err 信息并返回

func (*ErrCode) Errs

func (c *ErrCode) Errs() []error

Errs 返回 error 列表

func (*ErrCode) Msg

func (c *ErrCode) Msg() string

Msg 返回 msg 描述

func (*ErrCode) SetMsg

func (c *ErrCode) SetMsg(msg string) *ErrCode

SetMsg 更新 msg 字段

type GinStructValidator added in v1.0.1

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

GinStructValidator 自定义参数 binding 验证错误信息输出格式

func (*GinStructValidator) Engine added in v1.0.1

func (v *GinStructValidator) Engine() interface{}

Engine returns the underlying validator engine which powers the default Validator instance. This is useful if you want to register custom validations or struct level validations. See validator GoDoc for more info - https://godoc.org/gopkg.in/go-playground/validator.v8

func (*GinStructValidator) ValidateStruct added in v1.0.1

func (v *GinStructValidator) ValidateStruct(obj interface{}) error

ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.

type Hashids

type Hashids struct {
	HashID     *hashids.HashID
	HashIDData *hashids.HashIDData
	// contains filtered or unexported fields
}

Hashids 封装hashids方法

func MustNewHashids added in v1.0.34

func MustNewHashids(salt string, minLength int, prefix string) *Hashids

MustNewHashids 创建Hashids对象 salt可以使用用户创建记录时的用户唯一身份标识+当前时间戳的字符串作为值 minLength指定转换后的最小长度,随着数字ID的增大长度可能会变长

func NewHashids

func NewHashids(salt string, minLength int, prefix string) (*Hashids, error)

NewHashids 创建Hashids对象 salt可以使用用户创建记录时的用户唯一身份标识+当前时间戳的字符串作为值 minLength指定转换后的最小长度,随着数字ID的增大长度可能会变长

func (*Hashids) Decode

func (h *Hashids) Decode(hashID string) (int64, error)

Decode 将生成的随机字符串ID转为为原始的数字类型ID

func (*Hashids) Encode

func (h *Hashids) Encode(int64ID int64) (string, error)

Encode 将数字类型的ID转换为指定长度的随机字符串ID int64ID为需要转换的数字id,在没有自增主键ID时,可以采用当前用户已存在的记录数+1作为数字id,保证该数字在该用户下唯一

type JSONTime

type JSONTime struct {
	time.Time
}

JSONTime 用于在 json 中自定义时间格式 json marshal 一个带有 time.Time 字段类型的结构体时,时间格式固定为 RFC3339 格式 将 time.Time 类型替换为 JSONTime 类型,可设置时间格式为 JSONTimeFormat 中定义的格式

func NewJSONTime

func NewJSONTime(t time.Time) JSONTime

NewJSONTime 创建 JSONTime 对象

func (JSONTime) MarshalJSON

func (t JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON 使用 JSONTimeFormat 覆盖 time 包中实现的 json.Marshaler 接口

func (*JSONTime) Scan

func (t *JSONTime) Scan(v interface{}) error

Scan 在 gorm 中只重写 MarshalJSON 是不够的,只写这个方法会在写数据库的时候会提示 delete_at 字段不存在,需要加上 database/sql 的 Value 和 Scan 方法

func (JSONTime) String

func (t JSONTime) String() string

String 实现 String 方法 用于 print

func (JSONTime) Value

func (t JSONTime) Value() (driver.Value, error)

Value 在 gorm 中只重写 MarshalJSON 是不够的,只写这个方法会在写数据库的时候会提示 delete_at 字段不存在,需要加上 database/sql 的 Value 和 Scan 方法

type Pagination

type Pagination struct {
	// 当前页面数据开始下标
	StartIndex int `json:"start_index"`
	// 当前页面数据结束下标
	EndIndex int `json:"end_index"`
	// 数据总数
	TotalCount int `json:"total_count"`
	// 分页总数
	PagesCount int `json:"pages_count"`
	// 当前页码
	PageNum int `json:"page_num"`
	// 当前 offset
	PageOffset int `json:"page_offset"`
	// 上一页页码
	PrevPageNum int `json:"prev_page_num"`
	// 下一页页码
	NextPageNum int `json:"next_page_num"`
	// 分页大小
	PageSize int `json:"page_size"`
	// 是否有上一页
	HasPrev bool `json:"has_prev"`
	// 是否有下一页
	HasNext bool `json:"has_next"`
}

Pagination Paginate return it 异常数据时分页总数为 0 ,当前页码、上下页码均不判断逻辑,只管数值增减

func PaginateByOffsetLimit

func PaginateByOffsetLimit(totalCount, offset, limit int) Pagination

PaginateByOffsetLimit 按 offset,limit 计算分页信息

func PaginateByPageNumSize

func PaginateByPageNumSize(totalCount, pageNum, pageSize int) Pagination

PaginateByPageNumSize 按 pagenum,pagesize 计算分页信息 参数必须全部大于 0

Jump to

Keyboard shortcuts

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