gotool

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

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 26 Imported by: 0

README

GoTool

一个轻量级的Go语言工具集合,提倡的核心理念为:基于Golang原生库、无第三方依赖。

安装

go get -u github.com/up-zero/gotool

快速开始

例如 Md5() 方法,使用方式如下所示,其它方法参考功能列表及其测试案例。

package main

import "github.com/up-zero/gotool"
import "log"

func main() {
	data, err := gotool.Md5("123456")
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(data)
}

方法列表

  • Md5 获取md5值
  • Md5Iterations 迭代多次求md5
  • Md5File 获取文件的MD5
  • FileCopy 文件拷贝
  • FileDownload 文件下载
  • DirCopy 绝对目录文件拷贝
  • CurrentDirCount 当前文件夹下(不迭代子文件夹)文件或文件夹的个数
  • FileCount 获取指定目录下的文件个数
  • Zip 文件夹压缩
  • ZipWithNotify 带通知的文件夹压缩
  • Unzip 文件解压
  • UnzipWithNotify 带通知的文件解压
  • ExecShell 运行shell命令或脚本
  • ExecCommand 运行命令
  • ExecShellWithNotify 带通知的运行shell命令或脚本
  • ExecCommandWithNotify 带通知的运行命令
  • PsByName 根据程序名查询进程列表
  • HmacSHA256 计算 SHA256
  • HmacSHA384 计算 HmacSHA384
  • HmacSHA512 计算 HmacSHA512
  • JWTGenerate 生成JWT
  • JWTParse 解析JWT
  • RFC3339ToNormalTime RFC3339 日期格式标准化
  • RFC1123ToNormalTime RFC1123 日期格式标准化
  • If 三元运算符
  • ArrayDuplicate 数组去重
  • ArrayIn 数组是否包含某个值
  • ArrayJoin 整型拼接
  • RandomStr 随机字符串
  • RandomNumber 随机数字
  • RandomAlpha 随机字母
  • RandomAlphaNumber 随机字母数字
  • StrToInt64 字符串转换为int64
  • StrToUint64 字符串转换为uint64
  • StrToFloat64 字符串转换为float64
  • Int64ToStr int64转换为字符串
  • Uint64ToStr uint64转换为字符串
  • Float64ToStr float64转换为字符串
  • Int64ToHex int64转换为十六进制字符串
  • HexToInt64 十六进制字符串转换为int64
  • SendMail 发送邮件
  • Ipv4sLocal 获取本地ipv4地址
  • ShouldBindJson json入参绑定
  • ShouldBindQuery query入参绑定
  • UUID uuid
  • UUIDGenerate 生成UUID
  • SignalSnowflake 单节点的雪花码

Documentation

Index

Constants

View Source
const (
	DateTime = "2006-01-02 15:04:05"
	DateOnly = "2006-01-02"
	TimeOnly = "15:04:05"
)

Variables

View Source
var ErrDstSrcSame = errors.New("dst and src is same")

ErrDstSrcSame 目标和源是同一个

View Source
var ErrExpiredJwt = errors.New("expired jwt")

ErrExpiredJwt jwt 过期

View Source
var ErrInvalidJwtAlgorithm = errors.New("invalid jwt algorithm")

ErrInvalidJwtAlgorithm jwt 算法不支持

View Source
var ErrInvalidJwtFormat = errors.New("invalid jwt format")

ErrInvalidJwtFormat jwt 格式错误

View Source
var ErrInvalidJwtSignature = errors.New("invalid jwt signature")

ErrInvalidJwtSignature jwt 签名错误

View Source
var ErrNotIsDir = errors.New("not is dir")

ErrNotIsDir 不是目录

View Source
var ErrNotSupportType = errors.New("not support type")

ErrNotSupportType 不支持的类型

Mac 地址

Functions

func ArrayDuplicate

func ArrayDuplicate[T int64 | int32 | int16 | int8 | int | string](elems ...[]T) []T

ArrayDuplicate 数组去重

elems 传入同种类型数组

func ArrayIn

func ArrayIn[T int64 | int32 | int16 | int8 | int | uint | uint8 | uint16 | uint32 | uint64 | string |
	bool | float64 | float32](target T, arr []T) bool

ArrayIn 数组是否包含某个值

target 目标值 arr 待遍历的数组

func ArrayJoin

func ArrayJoin[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](elems []T, sep string) string

ArrayJoin 整型拼接

elems 待拼接的数值 sep 拼接用的字符串

func CurrentDirCount

func CurrentDirCount(dir string, args ...string) (int, error)

CurrentDirCount 当前文件夹下(不迭代子文件夹)文件或文件夹的个数

dir 目录路径

Examples:

gotool.CurrentDirCount("/home/xxx") // 当前文件夹下所有文件及文件夹的个数
gotool.CurrentDirCount("/home/xxx", "file") // 当前文件夹下文件的个数
gotool.CurrentDirCount("/home/xxx", "dir") // 当前文件夹下文件夹的个数

func DirCopy

func DirCopy(dst, src string) error

DirCopy 绝对目录文件拷贝

dst: 目标目录 src: 源目录

func ExecCommand

func ExecCommand(name string, arg ...string) error

ExecCommand 运行命令

name: 命令名称 arg: 命令参数

func ExecCommandWithNotify

func ExecCommandWithNotify(ch chan string, name string, arg ...string) error

ExecCommandWithNotify 带通知的运行命令

name: 命令名称 arg: 命令参数

func ExecShell

func ExecShell(shell string) error

ExecShell 运行shell命令或脚本

shell: shell 命令或脚本

func ExecShellWithNotify

func ExecShellWithNotify(ch chan string, shell string) error

ExecShellWithNotify 带通知的运行shell命令或脚本

ch: 输出通道 shell: shell 命令或脚本

func FileCopy

func FileCopy(dst, src string) error

FileCopy 文件拷贝

dst: 目标文件 src: 源文件

func FileCount

func FileCount(dir string, args ...string) (int, error)

FileCount 获取指定目录下的文件个数

dir 目录路径

Examples:

gotool.FileCount("/home/xxx") // 指定目录的文件个数
gotool.FileCount("/home/xxx", ".jpg") // 指定目录的指定后缀名的文件个数
gotool.FileCount("/home/xxx", ".jpg", ".png") // 指定目录的多个后缀名的文件个数

func FileDownload

func FileDownload(url, filePath string) error

FileDownload 文件下载

url 文件地址 filePath 文件路径

func Float64ToStr

func Float64ToStr(num float64, prec ...int) string

Float64ToStr float64转换为字符串

num: float64 prec: 精度, 及就是小数点后的位数, 默认为-1, 即不限制

func HexToInt64

func HexToInt64(str string) int64

HexToInt64 十六进制字符串转换为int64

str: 十六进制字符串

func HmacSHA256

func HmacSHA256(data []byte, key []byte) []byte

HmacSHA256 计算 SHA256

data 数据 key 密钥

func HmacSHA384

func HmacSHA384(data []byte, key []byte) []byte

HmacSHA384 计算 HmacSHA384

data 数据 key 密钥

func HmacSHA512

func HmacSHA512(data []byte, key []byte) []byte

HmacSHA512 计算 HmacSHA512

data 数据 key 密钥

func HttpDelete

func HttpDelete(url string, data []byte, header ...byte) ([]byte, error)

HttpDelete http delete 请求

url: 请求地址 data: 请求参数 header: 请求头

func HttpDeleteWithTimeout

func HttpDeleteWithTimeout(url string, data []byte, timeout time.Duration, header ...byte) ([]byte, error)

HttpDeleteWithTimeout http delete 请求带超时时长

url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头

func HttpGet

func HttpGet(url string, header ...byte) ([]byte, error)

HttpGet http get 请求

url: 请求地址 header: 请求头

func HttpGetWithTimeout

func HttpGetWithTimeout(url string, timeout time.Duration, header ...byte) ([]byte, error)

HttpGetWithTimeout http get 请求

url: 请求地址 timeout: 超时时间 header: 请求头

func HttpPost

func HttpPost(url string, data []byte, header ...byte) ([]byte, error)

HttpPost http post 请求

url: 请求地址 data: 请求参数 header: 请求头

func HttpPostWithTimeout

func HttpPostWithTimeout(url string, data []byte, timeout time.Duration, header ...byte) ([]byte, error)

HttpPostWithTimeout http post 请求

url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头

func HttpPut

func HttpPut(url string, data []byte, header ...byte) ([]byte, error)

HttpPut http put 请求

url: 请求地址 data: 请求参数 header: 请求头

func HttpPutWithTimeout

func HttpPutWithTimeout(url string, data []byte, timeout time.Duration, header ...byte) ([]byte, error)

HttpPutWithTimeout http put 请求

url: 请求地址 timeout: 超时时间 data: 请求参数 header: 请求头

func If

func If(condition bool, a, b any) any

If 三元运算符

condition 条件 a 条件为真时返回的值 b 条件为假时返回的值

func Int64ToHex

func Int64ToHex(num int64, args ...string) string

Int64ToHex int64转换为十六进制字符串

num: int64 args: 可选参数, 用于指定填充的位数以及填充的字

Examples:

gotool.Int64ToHex(15) // 返回 F
gotool.Int64ToHex(15, "08") // 返回 000F

func Int64ToStr

func Int64ToStr(num int64) string

Int64ToStr int64转换为字符串

num: int64

func Ipv4sLocal

func Ipv4sLocal() ([]string, error)

Ipv4sLocal 获取本地ipv4地址

func JWTGenerate

func JWTGenerate(claims JwtClaims, secretKey string, algorithm ...string) (string, error)

JWTGenerate 生成JWT

claims JWT声明 secretKey 密钥 algorithm 加密算法,默认为 HS256,可选值为 HS256、HS384、HS512

func Md5

func Md5(elem any, salt ...string) (string, error)

Md5 获取md5值

elem: string, []byte salt: 加盐

func Md5File

func Md5File(path string) (string, error)

Md5File 获取文件的MD5

path 文件路径

func Md5Iterations

func Md5Iterations(s any, iterations int) (string, error)

Md5Iterations 迭代多次求md5

elem: string, []byte iterations: 迭代次数

func RFC1123ToNormalTime

func RFC1123ToNormalTime(rfc1123 string) (string, error)

RFC1123ToNormalTime RFC1123 日期格式标准化

rfc1123 RFC1123日期格式,如 Mon, 02 Jan 2006 15:04:05 MST

func RFC3339ToNormalTime

func RFC3339ToNormalTime(rfc3339 string) (string, error)

RFC3339ToNormalTime RFC3339 日期格式标准化

rfc3339 RFC3339日期格式,如 2006-01-02T15:04:05Z07:00

func RandomAlpha

func RandomAlpha(length int) string

RandomAlpha 随机字母

length: 随机字母长度

func RandomAlphaNumber

func RandomAlphaNumber(length int) string

RandomAlphaNumber 随机字母数字

length: 随机字母数字长度

func RandomNumber

func RandomNumber(length int) string

RandomNumber 随机数字

length: 随机数字长度

func RandomStr

func RandomStr(str string, length int) string

RandomStr 随机字符串

str: 待随机的字符串 length: 随机字符串长度

func ShouldBindJson

func ShouldBindJson(req *http.Request, data any) error

ShouldBindJson json入参绑定

r *http.Request data any 待绑定数据结构体指针

func ShouldBindQuery

func ShouldBindQuery(req *http.Request, data any) error

ShouldBindQuery query入参绑定

r *http.Request data any 待绑定数据结构体指针

func SignalSnowflake

func SignalSnowflake() int64

SignalSnowflake 单节点的雪花码

func StrToFloat64

func StrToFloat64(str string) float64

StrToFloat64 字符串转换为float64

str: 字符串

func StrToInt64

func StrToInt64(str string) int64

StrToInt64 字符串转换为int64

str: 字符串

func StrToUint64

func StrToUint64(str string) uint64

StrToUint64 字符串转换为uint64

str: 字符串

func UUID

func UUID() (string, error)

UUID 生成uuid

func UUIDGenerate

func UUIDGenerate() string

UUIDGenerate UUID 生成

func Uint64ToStr

func Uint64ToStr(num uint64) string

Uint64ToStr uint64转换为字符串

num: uint64

func Unzip

func Unzip(dest, src string) error

Unzip 文件解压

dest 解压到的路径 例如:/var/xxx src 文件路径 例如:/var/xxx.zip

func UnzipWithNotify

func UnzipWithNotify(dest, src string, ch chan *UnzipNotify) error

UnzipWithNotify 带通知的文件解压

dest 解压到的路径 例如:/var/xxx src 文件路径 例如:/var/xxx.zip ch 用于通知解压进度

func Zip

func Zip(dest, src string) error

Zip 文件夹压缩

dest 压缩到的文件 例如:/var/xxx.zip src 源文件夹 例如:/var/xxx

func ZipWithNotify

func ZipWithNotify(dest, src string, ch chan int) error

ZipWithNotify 带通知的文件夹压缩

dest 压缩到的文件 例如:/var/xxx.zip src 源文件夹 例如:/var/xxx ch 用于通知压缩进度

Types

type JwtClaims

type JwtClaims struct {
	Expires int64          `json:"expires"` // 过期时间, 多少秒后过期, 0表示永不不过期
	Data    map[string]any `json:"data"`    // 数据
}

func JWTParse

func JWTParse(jwt string, secretKey string, algorithm ...string) (*JwtClaims, error)

JWTParse 解析JWT

jwt JWT字符串 secretKey 密钥 algorithm 加密算法,默认为 HS256,可选值为 HS256、HS384、HS512

type Mail

type Mail struct {
	Username string // 用户名
	Password string // 密码
	Addr     string // smtp地址, 例如 mail.example.com:smtp
	From     string // 发送者
}

func (Mail) SendMail

func (mail Mail) SendMail(receiveUsers []string, subject, content string) error

SendMail 发送邮件

receiveUsers: 接收者 subject: 主题 content: 正文

type Process

type Process struct {
	Pid  string `json:"pid"`  // 进程ID号
	PPid string `json:"ppid"` // 进程父级ID号
	Cmd  string `json:"cmd"`  // 执行的命令
}

func PsByName

func PsByName(name string) ([]Process, error)

PsByName 根据程序名查询进程列表

name: 程序名

type Snowflake

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

Snowflake 结构体

func NewSnowflake

func NewSnowflake(epoch int64, nodeID uint8) *Snowflake

NewSnowflake 创建 Snowflake 实例

epoch: 起始时间戳 nodeID: 节点ID

func (*Snowflake) GenerateSnowflake

func (s *Snowflake) GenerateSnowflake() int64

GenerateSnowflake 生成新的雪花码

type UnzipNotify

type UnzipNotify struct {
	Progress int    `json:"progress"` // 解压进度
	FPath    string `json:"f_path"`   // 解压文件的路径
}

Jump to

Keyboard shortcuts

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