utils

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FilePathExist = IsExistPath

FilePathExist 文件路径是否存在

Functions

func AbsPath added in v1.0.2

func AbsPath(skips ...int) string

AbsPath 绝对路径,可获取当前文件所在角色路径,skips设置可获取调用量上一级的

func AesDecrypt

func AesDecrypt(baseStr string, key []byte) (origData []byte, aesErr error)

AesDecrypt AES解密

func AesEncrypt

func AesEncrypt(origData, key []byte) (string, error)

AesEncrypt AES加密,CBC 注意:aes密钥key的长度必须为16,24,32个字符

func ArrayDifference added in v1.0.4

func ArrayDifference[T interface{}](slices ...[]T) []T

ArrayDifference 取数组差集

func ArrayIntersection added in v1.0.4

func ArrayIntersection[T interface{}](slices ...[]T) []T

ArrayIntersection 取数组交集

func ArrayUnion added in v1.0.4

func ArrayUnion[T interface{}](slices ...[]T) []T

ArrayUnion 取数组并集

func Base64Decode

func Base64Decode(s string) string

func Base64Encode

func Base64Encode(s string) string

func BasePath

func BasePath() string

BasePath 运行的程序所在目录

func BytesToString

func BytesToString(b []byte) string

BytesToString converts byte slice to string without a memory allocation.

func CBCDecrypt

func CBCDecrypt(sign string, key, iv []byte) (string, error)

CBCDecrypt CBC解密

func CBCEncrypt

func CBCEncrypt(text string, key []byte, iv []byte) (string, error)

CBCEncrypt CBC加密

func CapturePanic

func CapturePanic() (has bool)

func CompareVersion

func CompareVersion(ver, cVer string) int

CompareVersion 比较版本,-1:小于,0:相等,1:大于 v1.1.1 compare v1.2.1

func ConvertMoreTo

func ConvertMoreTo[T any, T1 any](list []T, ty []T1) []T1

ConvertMoreTo 转换为对应的接口(多个进行转换处理)

func CopyFile added in v1.0.2

func CopyFile(src, dst string) error

CopyFile 复制文件,从src复制到dst

func CopyMap added in v1.0.4

func CopyMap[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | string | struct{}, T1 interface{}](m map[T]T1) map[T]T1

CopyMap 复制map

func CopyMoreTo

func CopyMoreTo[T interface{}, T1 interface{}](data []*T, list []*T1) []*T1

CopyMoreTo 浅拷贝数组,data=>target

func CopyTo

func CopyTo[T interface{}, T1 interface{}](data *T, target *T1) *T1

CopyTo 浅拷贝,data=>target

func CurrentPath

func CurrentPath() string

CurrentPath 当前目录(与运行程序所在目录无关)

func DecodeBase64

func DecodeBase64(s string) ([]byte, error)

DecodeBase64 base64解码处理

func EncodeBase64

func EncodeBase64(b []byte) string

EncodeBase64 base64编码处理

func EnsureDir added in v1.0.2

func EnsureDir(dirPath string) error

EnsureDir 确保目录存在

func ErrConvertMoreTo

func ErrConvertMoreTo[T any, T1 any](list []T, ty []T1) ([]T1, error)

ErrConvertMoreTo 转换为对应的类型(多个进行转换处理),返回error

func ErrCopyTo

func ErrCopyTo[T interface{}, T1 interface{}](data *T, target *T1) (*T1, error)

ErrCopyTo 浅拷贝(范围error),data=>target

func FileExt

func FileExt(file string) string

FileExt 获取文件后缀

func FindStrBetweenStr

func FindStrBetweenStr(s string, left, right string) string

FindStrBetweenStr 查找left和right字符串中间的字符串

func FmtPageParams

func FmtPageParams(pageNum, pageSize int64) (int64, int64)

func GetAccessKey added in v1.0.1

func GetAccessKey() string

GetAccessKey 获取一个accessKey,通常为20个字符

func GetAccessSecret added in v1.0.1

func GetAccessSecret() string

GetAccessSecret 获取一个accessSecret,通常为30个字符

func GetDigProvider added in v1.0.2

func GetDigProvider[T interface{}](di *dig.Container) T

GetDigProvider 获取指定provider

func GetLocalIp

func GetLocalIp() (ipv4 string, err error)

GetLocalIp 获取本机网卡IP

func GetRequestIp

func GetRequestIp(r *http.Request) string

GetRequestIp returns request real ip.

func InDigProvider

func InDigProvider[T interface{}](di *dig.Container) bool

InDigProvider 是否在dig中已提供provider

func InStringArray

func InStringArray(s string, arr []string) bool

func Interface2Float64

func Interface2Float64(d interface{}) float64

Interface2Float64 任意值转float64

func Interface2Int64

func Interface2Int64(d interface{}) int64

Interface2Int64 任意值转int64

func InterfaceToArray

func InterfaceToArray(records interface{}) []interface{}

InterfaceToArray 转换任意interface{}为数组interface{}

func InterfaceToSqlString

func InterfaceToSqlString(a interface{}) string

func InterfaceToString added in v1.0.2

func InterfaceToString(value interface{}) string

InterfaceToString 将接口转换为字符串

func IsExistPath

func IsExistPath(path string) bool

IsExistPath 文件或目录是否存在

func IsNil

func IsNil(t interface{}) bool

IsNil 判断是否是nil

func KeepHasPrefix

func KeepHasPrefix(s string, prefix string) string

KeepHasPrefix 确保有前缀

func KeepHasSuffix

func KeepHasSuffix(s string, suffix string) string

KeepHasSuffix 确保有后缀

func LookupArray added in v1.0.2

func LookupArray[T any](list []T, f func(T, int))

LookupArray 转换为对应的接口(多个进行转换处理)

func LookupMap added in v1.0.2

func LookupMap[T string | int | int64, T1 any](m map[T]T1, f func(T, T1))

LookupMap 转换为对应的接口(多个进行转换处理)

func LoopDo

func LoopDo(f func() bool, dur time.Duration) func()

LoopDo 重复执行

func LoopDoI

func LoopDoI(f func(int64) bool, dur time.Duration) func()

LoopDoI 重复执行(执行包含序号i)

func LoopHandler

func LoopHandler[T interface{}](list []T, h func(T))

LoopHandler 循环处理

func MapKeys added in v1.0.2

func MapKeys[T interface{}](data map[string]T) []string

func Max

func Max[T int | int8 | int16 | int32 | int64 | float32 | float64](a T, b T) T

Max 取最大值

func MaxInt

func MaxInt(a int, b int) int

func MaxInt64

func MaxInt64(a int64, b int64) int64

func Md5Sum

func Md5Sum(s string) string

func MergeMap added in v1.0.1

func MergeMap[T interface{}](mList ...map[string]T) map[string]T

MergeMap 合并map

func MergeSlice added in v1.0.1

func MergeSlice[T interface{}](sList ...[]T) []T

MergeSlice 合并数组

func Min

func Min[T int | int8 | int16 | int32 | int64 | float32 | float64](a T, b T) T

Min 取最小值

func MinInt

func MinInt(a int, b int) int

func MinInt64

func MinInt64(a int64, b int64) int64

func MoreValueChooseFloat64

func MoreValueChooseFloat64(value float64, more ...float64) float64

MoreValueChooseFloat64 多个值选择string,若没有more则默认value,有more则选择more的第一个

func MoreValueChooseInt64

func MoreValueChooseInt64(value int64, more ...int64) int64

MoreValueChooseInt64 多个值选择int64,若没有more则默认value,有more则选择more的第一个

func MoreValueChooseString

func MoreValueChooseString(value string, more ...string) string

MoreValueChooseString 多个值选择string,若没有more则默认value,有more则选择more的第一个

func NewMd5Password

func NewMd5Password(password string, secret string) string

func NewRequestId

func NewRequestId() string

func NewTrueScopeRand

func NewTrueScopeRand() *mrand.Rand

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

PKCS7Padding 填充加密块

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) []byte

PKCS7UnPadding 清除加密块中填充字节

func PadLeftString

func PadLeftString(str string, l int, pad string) string

PadLeftString 左填充字符串至指定长度

func PadRightString

func PadRightString(str string, l int, pad string) string

PadRightString 右填充字符串至指定长度

func RandCode

func RandCode(l int, chars []string) string

func RandFigureCode

func RandFigureCode(l int) string

RandFigureCode 纯数字

func RandLetterCode

func RandLetterCode(l int) string

RandLetterCode 纯字母(大小写都包含)

func RandLetterFigureCode

func RandLetterFigureCode(l int) string

RandLetterFigureCode 字母(大小写都包含)+数字 如需纯大写或纯小写,自行转换 strings.ToLower

func RandNickname

func RandNickname() string

RandNickname 随机昵称

func RandOrderNoByDateTime

func RandOrderNoByDateTime(prefix string, useDate bool, useTime bool, randLen int) string

RandOrderNoByDateTime 按日期生成单据编号,类似: TX202404230714209992

func ReadYaml

func ReadYaml(data interface{}, yamlContent string) error

func ReadYamlFile

func ReadYamlFile(data interface{}, configPath string) error

func SaveMultipartFile added in v1.0.2

func SaveMultipartFile(header *multipart.FileHeader, uploadDir, fileName string) (string, error)

SaveMultipartFile 保存文件

func ScanDirFileDo

func ScanDirFileDo(dir string, fn func(file string, path string) error) error

ScanDirFileDo 扫描目录文件并执行操作

func ScanEmbedFsDo

func ScanEmbedFsDo(embFs embed.FS, fn func(file string, path string) error) error

ScanEmbedFsDo 扫描文件操作 注意:下划线文件不会被 embed.FS 嵌入打包进来

func ScopeCreateSQLv1916

func ScopeCreateSQLv1916(scope *gorm.Scope)

ScopeCreateSQLv1916 使用gorm scope生成create sql和vars(适用于gorm v1.9.16版本) copy from gorm source code scope.SQL, scope.SQLVars

func ScopeUpdateSQLv1916

func ScopeUpdateSQLv1916(scope *gorm.Scope)

ScopeUpdateSQLv1916 使用gorm scope生成update sql和vars(适用于gorm v1.9.16版本) copy from gorm source code scope.SQL, scope.SQLVars

func SetRequestBodyJson added in v1.0.2

func SetRequestBodyJson(req *http.Request, data interface{}) error

func Signature

func Signature(params ...string) string

Signature 签名

func SliceInt64ToMap

func SliceInt64ToMap(src []int64) map[int64]bool

SliceInt64ToMap 切片转map(去重)

func StringIntId added in v1.0.2

func StringIntId(s string) int

StringIntId 字符串转对应int

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without a memory allocation.

func TrueScopeRand

func TrueScopeRand[T int | int64 | float64](min T, max T) T

TrueScopeRand 真随机数,范围随机,min<= V <max

func TrueScopeRandNoSafe

func TrueScopeRandNoSafe(min int64, max int64) int64

TrueScopeRandNoSafe 真随机数(非并发安全) 全局使用一个rand的情况下会引发数组panic(未知原因)

func UnixMilliFixUnit added in v1.0.1

func UnixMilliFixUnit(timestamp int64, unit int64) int64

UnixMilliFixUnit 毫秒级时间戳按unit取整

func UnixMilliToMonth added in v1.0.1

func UnixMilliToMonth(timestamp int64, zone string) (int64, error)

UnixMilliToMonth 毫秒级时间戳转换为每月1号凌晨时间戳

func UnixMilliToYear added in v1.0.1

func UnixMilliToYear(timestamp int64, zone string) (int64, error)

UnixMilliToYear 毫秒级时间戳转换为每年1月1号凌晨时间戳

func UnixMilliToZeroHour added in v1.0.1

func UnixMilliToZeroHour(timestamp int64, zone string) (int64, error)

UnixMilliToZeroHour 毫秒级时间戳转换为每日凌晨时间戳

Types

type TaoBaoIpData

type TaoBaoIpData struct {
	QueryIp string `json:"queryIp"`
	Ip      string `json:"ip"`

	//国家
	CountyId interface{} `json:"county_id"`
	County   string      `json:"county"`

	//
	CountryId string `json:"country_id"`
	Country   string `json:"country"`
	//区域
	AreaId string `json:"area_id"`
	Area   string `json:"area"`
	//如浙江
	RegionId string `json:"region_id"`
	Region   string `json:"region"`
	//城市,如:杭州
	CityId string `json:"city_id"`
	City   string `json:"city"`

	//运营商,如电信
	IspId string `json:"isp_id"`
	Isp   string `json:"isp"`
}

func ParseIpByTaoBao

func ParseIpByTaoBao(ip string) (TaoBaoIpData, error)

ParseIpByTaoBao 解析ip来源(淘宝解析) 具体查看: https://ip.taobao.com/instructions 可能会有限流,使用时最好注意下sleep间隔

type TaoBaoIpResult

type TaoBaoIpResult struct {
	Code int          `json:"code"`
	Msg  string       `json:"msg"`
	Data TaoBaoIpData `json:"data"`
}

TaoBaoIpResult 淘宝ip来源解析请求结果,结果示例:

{
   "data": {
       "area": "",
       "country": "中国",
       "isp_id": "1000114",
       "queryIp": "210.75.225.254",
       "city": "北京",
       "ip": "210.75.225.254",
       "isp": "科技网",
       "county": "",
       "region_id": "110000",
       "area_id": "",
       "county_id": null,
       "region": "北京",
       "country_id": "CN",
       "city_id": "110100"
   },
   "msg": "query success",
   "code": 0
}

Jump to

Keyboard shortcuts

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