Documentation
¶
Index ¶
- Constants
- Variables
- func ArrayDuplicate[T Number | string](elems ...[]T) []T
- func ArrayIn[T Number | string | bool](target T, arr []T) bool
- func ArrayJoin[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](elems []T, sep string) string
- func CopyProperties(src, dst any) error
- func CurrentDirCount(dir string, args ...string) (int, error)
- func DirCopy(src, dst string) error
- func ExecCommand(name string, arg ...string) error
- func ExecCommandWithNotify(ch chan string, name string, arg ...string) error
- func ExecShell(shell string) error
- func ExecShellWithNotify(ch chan string, shell string) error
- func FileCopy(src, dst string) error
- func FileCount(dir string, args ...string) (int, error)
- func FileDownload(url, filePath string) error
- func FileMainName(filePath string) string
- func FileMove(srcFile, dstFile string) error
- func FileRead(filePath string, dst any) error
- func FileSave(p string, data any) error
- func FileSync(filePath string) error
- func Float64ToStr(num float64, prec ...int) string
- func GenerateCaptcha(text string) (image.Image, error)
- func HexToInt64(str string) int64
- func HmacSHA256(data []byte, key []byte) []byte
- func HmacSHA384(data []byte, key []byte) []byte
- func HmacSHA512(data []byte, key []byte) []byte
- func HttpDelete(url string, data any, header ...byte) ([]byte, error)
- func HttpDeleteWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
- func HttpGet(url string, header ...byte) ([]byte, error)
- func HttpGetWithTimeout(url string, timeout time.Duration, header ...byte) ([]byte, error)
- func HttpPost(url string, data any, header ...byte) ([]byte, error)
- func HttpPostWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
- func HttpPut(url string, data any, header ...byte) ([]byte, error)
- func HttpPutWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
- func If(condition bool, a, b any) any
- func ImageCompression(srcFile, dstFile string, quality int) error
- func Int64ToHex(num int64, args ...string) string
- func Int64ToStr(num int64) string
- func Ipv4sLocal() ([]string, error)
- func JWTGenerate(claims JwtClaims, secretKey string, algorithm ...string) (string, error)
- func MathAbs[T Number](v T) T
- func MathMax[T Number](arg ...T) T
- func MathMin[T Number](arg ...T) T
- func Md5(elem any, salt ...string) (string, error)
- func Md5File(path string) (string, error)
- func Md5Iterations(s any, iterations int) (string, error)
- func MkParentDir(filePath string) error
- func ParseResponse[T any](respBytes []byte, err error) (T, error)
- func RFC1123ToNormalTime(rfc1123 string) (string, error)
- func RFC3339ToNormalTime(rfc3339 string) (string, error)
- func RandomAlpha(length int) string
- func RandomAlphaNumber(length int) string
- func RandomNumber(length int) string
- func RandomRangeInt(minValue, maxValue int) int
- func RandomStr(str string, length int) string
- func ShouldBindJson(req *http.Request, data any) error
- func ShouldBindQuery(req *http.Request, data any) error
- func SignalSnowflake() int64
- func StrToFloat32(s string) float32
- func StrToFloat64(s string) float64
- func StrToInt16(s string) int16
- func StrToInt32(s string) int32
- func StrToInt64(s string) int64
- func StrToInt8(s string) int8
- func StrToUint16(s string) uint16
- func StrToUint32(s string) uint32
- func StrToUint64(s string) uint64
- func StrToUint8(s string) uint8
- func SysUptime() (int64, error)
- func UUID() (string, error)
- func UUIDGenerate() string
- func Uint64ToStr(num uint64) string
- func Unzip(src, dest string) error
- func UnzipWithNotify(src, dest string, ch chan *UnzipNotify) error
- func UrlBase(rawURL string) string
- func Zip(src, dest string) error
- func ZipWithNotify(src, dest string, ch chan int) error
- type DownloadProgress
- type ImageSizeReply
- type JwtClaims
- type Mail
- type Number
- type Process
- type Snowflake
- type Stream
- type TemperatureStat
- type UnzipNotify
Constants ¶
const ( DateTime = "2006-01-02 15:04:05" DateOnly = "2006-01-02" TimeOnly = "15:04:05" )
Variables ¶
var ( // Mac 地址 Mac string // EmptyMapBytes 空map EmptyMapBytes = []byte{0x7b, 0x7d} // DateTimeMilli 毫秒格式时间 DateTimeMilli = "2006-01-02 15:04:05.000" )
var ErrDstMustBePointerStruct = errors.New("dst must be a pointer struct")
ErrDstMustBePointerStruct 目标必须是指针结构体
var ErrDstSrcSame = errors.New("dst and src is same")
ErrDstSrcSame 目标和源是同一个
var ErrExpiredJwt = errors.New("expired jwt")
ErrExpiredJwt jwt 过期
var ErrInvalidJwtAlgorithm = errors.New("invalid jwt algorithm")
ErrInvalidJwtAlgorithm jwt 算法不支持
var ErrInvalidJwtFormat = errors.New("invalid jwt format")
ErrInvalidJwtFormat jwt 格式错误
var ErrInvalidJwtSignature = errors.New("invalid jwt signature")
ErrInvalidJwtSignature jwt 签名错误
var ErrInvalidUptimeFile = errors.New("invalid uptime file")
ErrInvalidUptimeFile uptime 文件无效
var ErrNotIsDir = errors.New("not is dir")
ErrNotIsDir 不是目录
var ErrNotSupportFormat = errors.New("not support format")
ErrNotSupportFormat 数据格式不支持
var ErrNotSupportType = errors.New("not support type")
ErrNotSupportType 不支持的类型
var ErrSrcDstCannotBeNil = errors.New("src and dst cannot be nil")
ErrSrcDstCannotBeNil 源和目标不能为空
var ImageExtMap = map[string]struct{}{
".jpg": {},
".jpeg": {},
".png": {},
".gif": {},
".bmp": {},
".webp": {},
".tif": {},
".tiff": {},
}
Functions ¶
func ArrayJoin ¶
func ArrayJoin[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](elems []T, sep string) string
ArrayJoin 整型拼接
elems 待拼接的数值 sep 拼接用的字符串
func CopyProperties ¶
CopyProperties 复制结构体的属性
src: 源对象 dst: 目标对象
注意:dst必须是指针类型,且指向的结构体类型与src类型相同
Examples: ¶
type src struct { Name string Map map[string]int } type dst struct { Name string Map map[string]int Age int } s1 := src{Name: "test", Map: map[string]int{"a": 1}} s2 := new(dst) gotool.CopyProperties(s1, s2)
func CurrentDirCount ¶
CurrentDirCount 当前文件夹下(不迭代子文件夹)文件或文件夹的个数
dir 目录路径
Examples:
gotool.CurrentDirCount("/home/xxx") // 当前文件夹下所有文件及文件夹的个数 gotool.CurrentDirCount("/home/xxx", "file") // 当前文件夹下文件的个数 gotool.CurrentDirCount("/home/xxx", "dir") // 当前文件夹下文件夹的个数
func FileCount ¶
FileCount 获取指定目录下的文件个数
dir 目录路径
Examples:
gotool.FileCount("/home/xxx") // 指定目录的文件个数 gotool.FileCount("/home/xxx", ".jpg") // 指定目录的指定后缀名的文件个数 gotool.FileCount("/home/xxx", ".jpg", ".png") // 指定目录的多个后缀名的文件个数
func FileMainName ¶
FileMainName 获取指定路径的文件名
filePath 文件路径或文件名
Examples:
gotool.FileMainName("/opt/gotool/test.go") // test
gotool.FileMainName("test.go") // test
func FileMove ¶
FileMove 文件移动
srcFile 源文件 dstFile 目标文件
Examples:
gotool.FileMove("/opt/gotool/test.txt", "/opt/gotool/test/rename.txt")
func FileSave ¶
FileSave 保存文件
Examples:
gotool.FileSave("/opt/gotool/test.txt", []byte("hello world")) gotool.FileSave("/opt/gotool/test.txt", struct{ Message string }{Message: "hello world"})
func GenerateCaptcha ¶
GenerateCaptcha 验证码图片生成(目前只支持数字)
text 验证码文本
Example 1: ¶
// image.Image to bytes img, _ := GenerateCaptcha("5679") var buf bytes.Buffer err := png.Encode(&buf, img) if err != nil { t.Fatal("Error encoding image:", err) } buf.Bytes()
func HttpDeleteWithTimeout ¶
func HttpDeleteWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
HttpDeleteWithTimeout http delete 请求带超时时长
url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头
func HttpPostWithTimeout ¶
func HttpPostWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
HttpPostWithTimeout http post 请求
url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头
func HttpPutWithTimeout ¶
func HttpPutWithTimeout(url string, data any, timeout time.Duration, header ...byte) ([]byte, error)
HttpPutWithTimeout http put 请求
url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头
func ImageCompression ¶
ImageCompression 图片压缩
srcFile 源图片路径 dstFile 目标图片路径 quality 压缩质量,范围 1-100(值越低,压缩率越高,质量越低),对于 PNG 图片,映射到 0-9 的压缩级别(0:无压缩,9:最大压缩)
func Int64ToHex ¶
Int64ToHex int64转换为十六进制字符串
num: int64 args: 可选参数, 用于指定填充的位数以及填充的字
Examples:
gotool.Int64ToHex(15) // 返回 F gotool.Int64ToHex(15, "08") // 返回 000F
func JWTGenerate ¶
JWTGenerate 生成JWT
claims JWT声明 secretKey 密钥 algorithm 加密算法,默认为 HS256,可选值为 HS256、HS384、HS512
func RFC1123ToNormalTime ¶
RFC1123ToNormalTime RFC1123 日期格式标准化
rfc1123 RFC1123日期格式,如 Mon, 02 Jan 2006 15:04:05 MST
func RFC3339ToNormalTime ¶
RFC3339ToNormalTime RFC3339 日期格式标准化
rfc3339 RFC3339日期格式,如 2006-01-02T15:04:05Z07:00
func UnzipWithNotify ¶
func UnzipWithNotify(src, dest string, ch chan *UnzipNotify) error
UnzipWithNotify 带通知的文件解压
src 文件路径 例如:/var/xxx.zip
dest 解压到的路径 例如:/var/xxx
ch 用于通知解压进度
func ZipWithNotify ¶
ZipWithNotify 带通知的文件夹压缩
src 源文件夹 例如:/var/xxx
dest 压缩到的文件 例如:/var/xxx.zip
ch 用于通知压缩进度
Types ¶
type DownloadProgress ¶
func FileDownloadWithNotify ¶
func FileDownloadWithNotify(ch chan DownloadProgress, url, filePath string) (*DownloadProgress, error)
FileDownloadWithNotify 带通知的文件下载
ch 通知进度 url 文件地址 filePath 文件路径
type ImageSizeReply ¶
func ImageSize ¶
func ImageSize(imagePath string) (*ImageSizeReply, error)
ImageSize 图片尺寸 说明:当图片类型不是标准库提供的,需要导入扩展库中的image golang.org/x/image
imagePath 图片路径
type JwtClaims ¶
type Mail ¶
type Process ¶
type Snowflake ¶
type Snowflake struct {
// contains filtered or unexported fields
}
Snowflake 结构体
func (*Snowflake) GenerateSnowflake ¶
GenerateSnowflake 生成新的雪花码
type Stream ¶
type Stream[T any] struct { // contains filtered or unexported fields }
func StreamMap ¶
StreamMap 数据处理与转换
Examples: ¶
s := NewStream([]int{1, 2, 3, 4, 5})
StreamMap(s, func(v int) string { return fmt.Sprintf("data: %d", v) })
func (*Stream[T]) Filter ¶
Filter 数据过滤
Examples: ¶
s := NewStream([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
s.Filter(func(v int) bool { return v%2 == 0 }) // []int{2, 4, 6, 8, 10}
func (*Stream[T]) Map ¶
Map 数据处理
Examples: ¶
s := NewStream([]int{1, 2, 3, 4, 5})
s.Map(func(v int) int { return v * 2 })
type TemperatureStat ¶
type UnzipNotify ¶
Source Files
¶
- array.go
- bean.go
- const.go
- conv.go
- dir.go
- errors.go
- exec.go
- exec_linux.go
- file.go
- font.go
- hmac.go
- http.go
- if.go
- image.go
- image_types.go
- ip.go
- jwt.go
- mail.go
- math.go
- md5.go
- ps.go
- ps_linux.go
- random.go
- request.go
- snow.go
- stream.go
- sys_linux.go
- temperature.go
- temperature_linux.go
- time.go
- url.go
- uuid.go
- zip.go