putil

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 46 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIDMgr = NewIDManager(100000)
View Source
var Interact _interact
View Source
var PgoTraceIDKey = pgoTraceID{}
View Source
var PgoUserIDKey = pgoUserID{}

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte) ([]byte, error)

func AesDecryptFromBase64

func AesDecryptFromBase64(b64 string, key []byte) ([]byte, error)

func AesEncrypt

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

func AesEncryptToBase64

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

func AnyIsNil added in v0.0.6

func AnyIsNil(val any) bool

接口变量只有在 类型(Type)和值(Value)都为 nil 时,才被认为是 nil

func AnyToInt32 added in v0.0.6

func AnyToInt32(val any, defaultVal int32) int32

--------------------------------------------------

func AnyToStr added in v0.0.6

func AnyToStr(val any) string

func BoolToStr

func BoolToStr(value bool) string

BoolToStr converts bool to string.

func ByteToStr

func ByteToStr(value byte) string

ByteToStr converts byte to string.

func Bytes2Length

func Bytes2Length(ret []byte) int64

Bytes2Length converts a byte array to an int64 value.

func Conv_gb2312_utf8 added in v0.0.6

func Conv_gb2312_utf8(s string) (string, error)

func Conv_gbk2utf8 added in v0.0.6

func Conv_gbk2utf8(s string) (string, error)

func Conv_utf16be_utf8 added in v0.0.6

func Conv_utf16be_utf8(s string) (string, error)

func Conv_utf16le_utf8 added in v0.0.6

func Conv_utf16le_utf8(s string) (string, error)

func Crc32

func Crc32(src string) (string, error)

Crc32 returns file's crc32 string.

func DateFromStrDefault added in v0.0.6

func DateFromStrDefault(dateStr string) (time.Time, error)

-------------------------------------------------- "YYYYMMDD"

func DateToStrDefault added in v0.0.6

func DateToStrDefault(t time.Time) string

"YYYYMMDD"

func DecimalToStr added in v0.0.7

func DecimalToStr(d decimal.Decimal) string

func DecodeBase64

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

DecodeBase64 decode a base64 string.

func DecodeBase64Str

func DecodeBase64Str(input string) string

DecodeBase64Str decode a base64 string.

func DecodeHex

func DecodeHex(input string) ([]byte, error)

func EncodeBase64

func EncodeBase64(input string) string

-------------------------------------------------- EncodeBase64 converts an input string to base64 string.

func EncodeHex

func EncodeHex(input []byte) string

--------------------------------------------------

func Exec added in v0.0.6

func Exec(name string, args ...string) (string, error)

func ExecBG deprecated added in v0.0.6

func ExecBG(command string) error

Deprecated: 废弃,linux上应该用systemctl或pm2托管

func ExecSplit added in v0.0.6

func ExecSplit(command string) (string, error)

linux上支持使用管道,将用/bin/sh -c来执行

func FillPrefixToLen

func FillPrefixToLen(in, prefix string, length int) string

func Float32ToStr

func Float32ToStr(value float32) string

Float32ToStr converts float32 to string.

func Float32ToStrPrec2

func Float32ToStrPrec2(value float32) string

Float32ToStr converts float32 to string. Keep prec of 2

func Float64ToStr

func Float64ToStr(value float64) string

Float64ToStr converts float64 to string.

func Float64ToStrByPrec

func Float64ToStrByPrec(f float64, prec int) string

func Float64ToStrPrec2

func Float64ToStrPrec2(f float64) string

Float64ToStr converts float64 to string. Keep prec of 2

func GetCallerFuncName

func GetCallerFuncName(skip int) string

func GetCurDir

func GetCurDir() string

os.Getwd() 获取当前工作目录

func GetExecFolder

func GetExecFolder() string

获取当前程序/可执行文件,所在的文件夹,的绝对路径

func GetExecName

func GetExecName() (n string)

func GetExecPath

func GetExecPath() string

获取当前程序/可执行文件,所在的绝对路径

func GetFileMd5

func GetFileMd5(fi string) (string, error)

GetFileMd5 gets file's md5.

func GetFileMd5Stream

func GetFileMd5Stream(f io.Reader) (string, error)

GetFileMd5Stream gets file's md5 by io.Reader

func GetFuncBaseName added in v0.0.6

func GetFuncBaseName(fn string) string

func GetFuncName

func GetFuncName(i any) string

func GetRand

func GetRand(start, end int) int

[start, end)

func GetRandStr

func GetRandStr(n int) string

func GetSha256

func GetSha256(data []byte) string

func GetTraceIdFromCtx added in v0.0.6

func GetTraceIdFromCtx(ctx context.Context) (string, bool)

func GetUrlQueryString added in v0.0.5

func GetUrlQueryString(req any) (querys map[string]string)

func GetUserIdFromCtx added in v0.0.6

func GetUserIdFromCtx(ctx context.Context) (int32, bool)

func HmacSha256

func HmacSha256(data, secret string) []byte

--------------------------------------------------

func HttpDo

func HttpDo(req *http.Request) (bodyBytes []byte, err error)

func Int8ToStr

func Int8ToStr(value int8) string

Int8ToStr converts int32 to string.

func Int16ToStr

func Int16ToStr(value int16) string

Int16ToStr converts int32 to string.

func Int32ListExcept

func Int32ListExcept(baseList []int32, exclude []int32) (ret []int32)

差集:输出,baseList中有,exclude中没有,的元素

func Int32ListIntersect

func Int32ListIntersect(list1 []int32, list2 []int32) (ret []int32)

int32数组交集,输出排序是根据nums2的排序

func Int32ListSafeGet added in v0.0.6

func Int32ListSafeGet(list []int32, index int) (ret int32)

func Int32ListToStr

func Int32ListToStr(Int32List []int32, split string) string

-------------------------------------------------- Int32ListToStr []int32 to string

func Int32ListToStrWithDelimiter

func Int32ListToStrWithDelimiter(Int32List []int32, split, left, right string) string

--------------------------------------------------

func Int32ListUnion

func Int32ListUnion(list1 []int32, list2 []int32) []int32

Int32ListUnion int32数组并集,去除重复数据

func Int32ListUnique

func Int32ListUnique(list []int32) []int32

Int32ListUnique int32数组去重

func Int32ToStr

func Int32ToStr(value int32) string

Int32ToStr converts int32 to string.

func Int64ToStr

func Int64ToStr(value int64) string

Int64ToStr converts int64 to string.

func IntToStr

func IntToStr(value int) string

IntToStr converts int to string.

func IsFuncParamPB added in v0.0.5

func IsFuncParamPB(f any) bool

--------------------------------------------------

func IsPB added in v0.0.5

func IsPB(t reflect.Type) bool

func Length2Bytes

func Length2Bytes(len int64, buffer []byte) []byte

-------------------------------------------------- Length2Bytes converts an int64 value to a byte array.

func Md5Sum

func Md5Sum(input ...string) string

-------------------------------------------------- Md5Sum calculates md5 value of some strings.

func MessageToCleanString added in v0.0.5

func MessageToCleanString(msg proto.Message, str *string) error

func MessageToString added in v0.0.5

func MessageToString(msg proto.Message, str *string) error

func NewHttpRequest

func NewHttpRequest(method, rawURL string, header, querys map[string]string, body string) (*http.Request, error)

func NewHttpRequestJson

func NewHttpRequestJson(method, rawURL string, header, querys map[string]string, body any) (*http.Request, error)

func NewPath added in v0.0.6

func NewPath(p string) *pathHelper

NewPath 创建 PathHelper

func NewPathS added in v0.0.7

func NewPathS(p string) *pathHelper

Simple

func PKCS7Padding

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

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) []byte

func ReplaceByStringMap

func ReplaceByStringMap(str string, replaceMap map[string]string) string

根据替换映射表替换字符串,要注意map的数据,不要造成循环替换

func RsaDecryptFromBase64

func RsaDecryptFromBase64(priKey *rsa.PrivateKey, encryptedMsg64 string) string

func RsaEncryptToBase64

func RsaEncryptToBase64(pubKey *rsa.PublicKey, msg string) string

func RsaGetPriKey

func RsaGetPriKey(priKeyBytes []byte) *rsa.PrivateKey

func RsaGetPubKey

func RsaGetPubKey(pubKeyBytes []byte) *rsa.PublicKey

func SetTraceIdToCtx added in v0.0.6

func SetTraceIdToCtx(ctx context.Context, traceId string) context.Context

func SetUserIdToCtx added in v0.0.6

func SetUserIdToCtx(ctx context.Context, userId int32) context.Context

func StrFirstToLower

func StrFirstToLower(str string) string

func StrFirstToUpper

func StrFirstToUpper(str string) string

func StrListExcept

func StrListExcept(baseList []string, exclude []string) (ret []string)

差集:输出,baseList中有,exclude中没有,的元素

func StrListIntersect added in v0.0.6

func StrListIntersect(list1 []string, list2 []string) (ret []string)

func StrListSafeGet added in v0.0.6

func StrListSafeGet(list []string, index int) (ret string)

func StrListToStr

func StrListToStr(stringList []string, split string) string

-------------------------------------------------- StringListToStr []string to string

func StrListUnion

func StrListUnion(list1 []string, list2 []string) []string

StrListUnion string数组并集,去除重复数据

func StrListUnique

func StrListUnique(list []string) []string

StrListUnique string数组去重

func StrListXOR

func StrListXOR(list1 []string, list2 []string) (ret []string)

取名XOR异或,实际逻辑是(A-B)U(B-A)的集合b https://www.lodashjs.com/docs/lodash.xor

func StrPrefixByNum added in v0.0.6

func StrPrefixByNum(str string, num int) string

func StrToBool

func StrToBool(value string) (bool, error)

StrToBool converts string to bool.

func StrToByte

func StrToByte(value string) (byte, error)

StrToByte converts string to byte.

func StrToCamelCase

func StrToCamelCase(str string) string

func StrToDecimal added in v0.0.7

func StrToDecimal(s string) decimal.Decimal

--------------------------------------------------

func StrToFloat32

func StrToFloat32(value string) (float32, error)

StrToFloat32 converts string to float32.

func StrToFloat64

func StrToFloat64(value string) (float64, error)

StrToFloat64 converts string to float64.

func StrToFloat64WithDefault

func StrToFloat64WithDefault(value string, d float64) float64

StrToFloat64 converts string to float64.

func StrToInt

func StrToInt(value string) (int, error)

StrToInt converts string to int.

func StrToInt8

func StrToInt8(value string) (int8, error)

StrToInt8 converts string to int8.

func StrToInt16

func StrToInt16(value string) (int16, error)

StrToInt16 converts string to int16.

func StrToInt32

func StrToInt32(value string) (int32, error)

StrToInt32 converts string to int32.

func StrToInt32List

func StrToInt32List(str string, split string) (ret []int32, err error)

StrToInt32List string to []int32

func StrToInt32List2

func StrToInt32List2(str, start, end, split string) ([]int32, error)

func StrToInt32ListWithDelimiter

func StrToInt32ListWithDelimiter(str, split, left, right string) (ret []int32, err error)

StrToInt32List string to []int32

func StrToInt32WithDefault

func StrToInt32WithDefault(value string, defaultVal int32) int32

StrToInt32 converts string to int32.

func StrToInt64

func StrToInt64(value string) (int64, error)

StrToInt64 converts string to int64.

func StrToInt64WithDefault

func StrToInt64WithDefault(value string, defaultVal int64) int64

StrToInt64 converts string to int64.

func StrToLowerCamelCase added in v0.0.7

func StrToLowerCamelCase(s string) string

func StrToStrList

func StrToStrList(str string, split string) (ret []string)

StrToStringList string to []string, ignore empty string item

func StrToStrListByStartAndEnd

func StrToStrListByStartAndEnd(s string, splitStart string, splitEnd string) (ret []string)

func StrToStrListWithEmpty

func StrToStrListWithEmpty(str string, split string) (ret []string)

StrToStringList string to []string, include empty string item

func StrToStrListWithSplit

func StrToStrListWithSplit(str string, split string) (ret []string)

StrToStrList基础上,分割后不删除split字符

func StrToUint

func StrToUint(value string) (uint, error)

StrToInt converts string to int.

func StrToUint8

func StrToUint8(value string) (uint8, error)

StrToUint8 converts string to uint8.

func StrToUint16

func StrToUint16(value string) (uint16, error)

StrToUint16 converts string to int16.

func StrToUint32

func StrToUint32(value string) (uint32, error)

StrToUint32 converts string to uint32.

func StrToUint64

func StrToUint64(value string) (uint64, error)

StrToUint64 converts string to uint64.

func StrToUpperCamelCase

func StrToUpperCamelCase(s string) string

func StringToMessage added in v0.0.5

func StringToMessage(str *string, msg proto.Message) error

--------------------------------------------------

func TimeFromStr

func TimeFromStr(timeStr, format string) (time.Time, error)

-------------------------------------------------- format sample: "YYYYMMDDTHH:mm:ss"

func TimeFromStrDefault

func TimeFromStrDefault(timeStr string) (time.Time, error)

-------------------------------------------------- "YYYYMMDDTHH:mm:ss"

func TimeToStr

func TimeToStr(t time.Time, format string) string

format sample: "YYYYMMDDTHH:mm:ss"

func TimeToStrDefault

func TimeToStrDefault(t time.Time) string

"YYYYMMDDTHH:mm:ss"

func UUID added in v0.0.5

func UUID() string

--------------------------------------------------

func UUID_S added in v0.0.5

func UUID_S() string

截断的短版UUID

func Uint8ToStr

func Uint8ToStr(value uint8) string

Uint8ToStr converts int32 to string.

func Uint16ToStr

func Uint16ToStr(value uint16) string

Uint16ToStr converts int32 to string.

func Uint32ToStr

func Uint32ToStr(value uint32) string

Uint32ToStr converts int32 to string.

func Uint64ToStr

func Uint64ToStr(value uint64) string

Uint64ToStr converts uint64 to string.

func UintToStr

func UintToStr(value uint) string

UintToStr converts int to string.

func WalkSliceByStep added in v0.0.5

func WalkSliceByStep(x any, step int, cb func(s, e int) error) (err error)

Types

type FileType added in v0.0.6

type FileType int

FileType 表示文件类型枚举

const (
	FileTypeOther FileType = iota
	FileTypeImage
	FileTypeVideo
)

type IDManager added in v0.0.5

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

func NewIDManager added in v0.0.5

func NewIDManager(initMaxId int32) *IDManager

func (*IDManager) GetNewSmallestAndUniqueID added in v0.0.5

func (m *IDManager) GetNewSmallestAndUniqueID() int32

获取最小但不重复的ID

func (*IDManager) ReleaseID added in v0.0.5

func (m *IDManager) ReleaseID(id int32)

type SshClient added in v0.0.7

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

func NewSSHClient added in v0.0.7

func NewSSHClient(user, password, addr string) (*SshClient, error)

func (*SshClient) Close added in v0.0.7

func (s *SshClient) Close()

func (*SshClient) GetAddr added in v0.0.7

func (s *SshClient) GetAddr() string

func (*SshClient) InitSftp added in v0.0.7

func (s *SshClient) InitSftp() error

--------------------------------------------------

func (*SshClient) PowershellCmd added in v0.0.7

func (s *SshClient) PowershellCmd(cmd string,
) (string, string, error)

func (*SshClient) RunCommand added in v0.0.7

func (s *SshClient) RunCommand(cmd string,
) (string, string, error)

func (*SshClient) Scp added in v0.0.7

func (s *SshClient) Scp(src, dst string) error

Jump to

Keyboard shortcuts

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