util

package module
v0.0.0-...-dd1c050 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 23 Imported by: 8

README

some golang function in my development 一些我开发时所用的golang函数

usage example see *_test.go 使用方法参见 *_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthCode

func AuthCode(str string, operation string, key string, expiry int64) string

AuthCode Comsenz discuz classical symmetric encryption/decryption function, example see authcode_test.go#TestAuthCode() AuthCode 康盛Discuz! 经典对称加解密函数,用法参考 authcode_test.go#TestAuthCode()

func Base64Decode

func Base64Decode(base64Str string) (content string)

Base64Decode base64解码 http://php.net/manual/en/function.base64-decode.php

func BasicTypeReflectSetValue

func BasicTypeReflectSetValue(rv reflect.Value, value string) (err error)

BasicTypeReflectSetValue use reflect.Value set value, main used for function DefaultValue(), usage see struct_test.go#TestBasicTypeReflectSetValue(),current not support pointer types, BasicTypeReflectSetValue 使用reflect.Value来修改变量的值,主要用于函数DefaultValue(),使用示范见struct_test.go#TestDefaultValue(),当前不支持指针类型变量

func BigToLittleEndian

func BigToLittleEndian[T int | uint64 | uint32 | uint16](bigEndian T) (result T)

func BoolToInt

func BoolToInt(value bool) int

func BoolToStrNumber

func BoolToStrNumber(value bool) string

func Date

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

Date php style date format Date PHP风格的日格式化

func DefaultValue

func DefaultValue(data interface{}) (err error)

DefaultValue simply set default value to struct field by "default" tag current support struct field types [ float32 float64 uint int uint8 int8 uint16 int16 uint32 int32 uint64 int64 bool string slice array map ],current not support pointer types, not support unexported field(lower case named field), usage example see struct_test.go#TestDefaultValue() DefaultValue 简单的设置默认值给结构体字段,通过default标签 当前支持的结构体字段类型 [ float32 float64 uint int uint8 int8 uint16 int16 uint32 int32 uint64 int64 bool string slice array map ],当前不支持指针类型变量,不支持未导出的结构体字段(名字小写的字段) 使用示范见struct_test.go#TestDefaultValue()

func DeleteByIndex

func DeleteByIndex(slice interface{}, index int) (err error)

DeleteByIndex delete slice element from slice by index Dont use this function on big slice Example see slice_test.go#TestDeleteByIndex() DeleteByIndex 以指定的索引下标删除slice中的某个元素,请勿用于巨大的slice

func DeleteByValue

func DeleteByValue(slice interface{}, value interface{}) (err error)

DeleteByValue delete slice element from slice by value if value matched multiple elements, theese elements will be delete from slice Dont use this function on big slice Example see slice_test.go#TestDeleteByValue() DeleteByValue 以指定的值标删除slice中的N个元素,请勿用于巨大的slice

func EnvOrDefault

func EnvOrDefault(envName string, defaultValue string) string

EnvOrDefault read OS environment variable name, if value is "", then return defaultValue EnvOrDefault 读取环境变量名的值,如果为空则返回第二个参数的值,否则直接返回换将变量名对应的值

func ErrorCaller

func ErrorCaller(err error) error

ErrorCaller insert caller to error info, inlucde package name and function name ErrorCaller 将调用处的包名和函数追加到error信息中

func EscapeWindowsFilename

func EscapeWindowsFilename(filename string) string

EscapeWindowsFilename escape special character for windows name EscapeWindowsFilename 转义windows文件名特殊字符

func FileExists

func FileExists(filePath string) bool

FileExists check file exists FileExists 查询一个文件是否存在

func FileGetContents

func FileGetContents(filename string) (content string)

FileGetContents read file into a string variable FileGetContents 把一个文件读成一个字符串 http://php.net/manual/en/function.file-get-contents.php

func GbkToUtf8

func GbkToUtf8(s []byte) ([]byte, error)

GbkToUtf8 convert gbk bytes content to utf8 bytes content GbkToUtf8 将GBK内容转换成UTF8

func IP2Long

func IP2Long(ip string) (*big.Int, int64, error)

IP2Long ip字符串转换为整数 IP2Long ip string to number from https://blog.csdn.net/janbar/article/details/127709072

func InSlice

func InSlice(slice interface{}, value interface{}) (exists bool, err error)

InSlice check if element exists in a slice value type and slice element type must be same Dont use this function on big slice Example see slice_test.go#TestInSlice() InSlice 检测值是否存在于slice中

func IsBigEndianCPU

func IsBigEndianCPU() bool

IsBigEndianCPU 检测主机内存字节序,一般来说power PC 这些IBM的CPU是大端序内存布局,而intel的CPU则是小端序布局。只有超过2个字节的数据类型才会有端序的概念,因此这里不检查uint8

func IsEmptyDir

func IsEmptyDir(dirPath string) bool

IsEmptyDir check path if is a empty directory IsEmptyDir 查询一个目录是否是空目录

func LittleToBigEndian

func LittleToBigEndian[T int | uint64 | uint32 | uint16](littleEndian T) (result T)

func LogJSON

func LogJSON(value interface{})

LogJSON print object as json string on console

func Long2IP

func Long2IP(ip *big.Int, ipv4 int64) string

Long2IP ip整数转换为字符串 Long2IP ip number to string from https://blog.csdn.net/janbar/article/details/127709072

func Md5

func Md5(s string) string

Md5 generate 32 length md5 string Md5 生成32位md5字串

func PHPDate

func PHPDate(stamp int64, format string) string

PHPDate php style date format PHPDate PHP风格的日期格式化

func PackInt

func PackInt[T int32 | int16 | int8 | uint32 | uint16 | uint8](hightBitNumber T, lowBitNumnber T) (packedNumber uint64)

func ParseStructWithEnv

func ParseStructWithEnv(structNode interface{}, rootNodeName string)

func ReadDirRecusive

func ReadDirRecusive(path string, callback func(filePath string) (err error)) (err error)

ReadDirRecusive read files under directory with recursive ReadDirRecusive 递归读取目录下所有的文件和文件夹

func RegisterSignalCallback

func RegisterSignalCallback(callback func(), signals ...os.Signal)

RegisterSignalCallback watch system signal and do callback function , this function will stuck current goroutine before received signal RegisterSignalCallback 监听系统系统并执行回调,如果没有收到信号则卡住当前goroutine

func SlicePager

func SlicePager(input interface{}, outut interface{}, p *SlicePagerOpts) (err error)

SlicePager paginate slice by SlicePagerOpts SlicePager 基于slice做分页

func StrToEnvName

func StrToEnvName(s string) (ret string)

StrToEnvName cover string to linux style environment variable name, only [A-Za-z\-_\.] will be convert to underscore StrToEnvName 将字符串转换为linux环境变量名称 只收录字符 [A-Za-z\-_\.] 点号会被转转为下划线

func StringDefault

func StringDefault(source *string, defaultValue string)

StringDefault if string variable value is empty, give it default value StringDefault 如果字符串值为空,则给予默认值

func StripHtmlTags

func StripHtmlTags(html string) (s string)

StripHtmlTags remove html tags and return text content only StripHtmlTags 去除html标签并返回纯文本内容

func SubStr

func SubStr(str string, begin, length int) (substr string)

SubStr get sub string by offset SubStr 截字

func Utf8ToGbk

func Utf8ToGbk(s []byte) ([]byte, error)

Utf8ToGbk convert utf8 bytes content to gbk bytes content Utf8ToGbk 将UTF8内容转换为GBK

Types

type DockerImageNameInfo

type DockerImageNameInfo struct {
	// Schema api server http schema, values are http or https
	// Schema 仓库服务器访问方式,http或https
	Schema string `json:"schema"`
	// Domain api server host domain
	// Domain 仓库服务器域名
	Domain string `json:"domain"`
	// Path image base name, such as rumia/rds-operator or pizza/rumia/rds-operator or library/centos
	// Path 镜像基本名称,比如 rumia/rds-operator or pizza/rumia/rds-operator or library/centos
	Path string `json:"path"`
	// Tag image tag, exclusive with Digest column
	// Tag 镜像tag,和sha256签名互斥
	Tag string `json:"tag"`
	// Digest image sha256 digest, exclusive with Tag column
	// Digest 镜像sha256签名,和tag字段互斥
	Digest string `json:"digest"`
}

DockerImageNameInfo docker image information DockerImageNameInfo docker image 信息

func ParseDockerImageNameInfo

func ParseDockerImageNameInfo(imageName string) (info *DockerImageNameInfo)

ParseDockerImageNameInfo parse docker image info by image name ParseDockerImageNameInfo 通过镜像名称解析docker镜像信息

func (*DockerImageNameInfo) GetReference

func (t *DockerImageNameInfo) GetReference() (tagOrDigest string)

GetReference return sha256 digest or tag name , always used for docker registry v2 client GetReference 返回sha256签名或tag名称 , 通常用于 docker registry v2 客户端

func (*DockerImageNameInfo) String

func (t *DockerImageNameInfo) String() (fullname string)

String format output String 格式化输出

type SlicePagerOpts

type SlicePagerOpts struct {
	// CurrentPage current page
	// CurrentPage 当前页
	CurrentPage int `json:"currentPage"`
	// TotalPages total pages
	// TotalPages 总页数
	TotalPages int `json:"totalPages"`
	// TotalNum total elements count of slice
	// TotalNum 总共有多少条数据在slice
	TotalNum int `json:"totalNum"`
	// Limit how many slice elements per page
	// Limit 每页将有多少条数据
	Limit int `json:"limit"`
}

SlicePagerOpts pagination settings SlicePagerOpts 分页设置

type Version

type Version struct {
	// Major major verion is always with some breaking changes
	// Major 主版本通常是带有不可逆的功能变更
	Major int
	// Minor minor version is always with some compatible function changes in major version
	// Minor 次要版本通常不会发生不可逆功能变更,主要是新增一些功能
	Minor int
	// Bugfix bugfix version is always with some bug fix, no any breaking change or function change
	// Bugfix bug修复版本通常只是修复bug,不会发生功能新增或更改
	Bugfix string
}

func ParseVersion

func ParseVersion(versionStr string) (version *Version, err error)

ParseVersion support version styles are v0.0.0 and 0.0.0 ParseVersion 解析版本号字符串

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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