utils

package
v0.8.16 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TIME_PRECISION_SECOND      = iota // 定义秒精度常量
	TIME_PRECISION_MILLISECOND = iota // 定义毫秒精度常量
	TIME_PRECISION_MICROSECOND = iota // 定义微秒精度常量
	TIME_PRECISION_NANOSECOND  = iota // 定义纳秒精度常量
)

Variables

View Source
var (
	ExecFilename string // 可执行文件名
	ExecPath     string // 可执行文件路径
	LocalDir     string // 可执行文件所在目录
)

Functions

func AesDecrypt added in v0.1.5

func AesDecrypt(encryptedHex string, key []byte) (string, error)

AesDecrypt Aes解密

func AesEncrypt added in v0.1.5

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

AesEncrypt Aes加密

func BitsToSubNetMask added in v0.1.8

func BitsToSubNetMask(maskBits int) (string, error)

BitsToSubNetMask 将掩码位数转换为子网掩码 传入 24,返回 255.255.255.0

func CalculateNetwork added in v0.1.8

func CalculateNetwork(ipAddr, subnetMask string) (string, error)

CalculateNetwork 根据给定的IP地址和子网掩码计算网络地址 传入 192.168.1.1 和 255.255.255.0,返回 192.168.1.0/24

func ConcatStrings added in v0.2.8

func ConcatStrings(parts ...string) string

ConcatStrings 使用strings.Builder高效地拼接多个字符串

func CopySlice added in v0.3.2

func CopySlice[T any](original []T) []T

CopySlice 复制切片为不同的引用(深拷贝) T为切片元素

func DetectTcpPort added in v0.1.3

func DetectTcpPort(ip string, port int, timeout time.Duration) bool

DetectTcpPort 检测Tcp端口是否开通

func GetCurrentTimestampMinute added in v0.6.0

func GetCurrentTimestampMinute() int64

GetCurrentTimestampMinute 获取当前时间的整数分钟,返回Unix时间戳秒数

func GetFilenameWithoutExt added in v0.7.3

func GetFilenameWithoutExt(filename string) string

GetFilenameWithoutExt 返回不带扩展名的文件名。

func HMACMD5Hash added in v0.1.5

func HMACMD5Hash(text string, key string) string

HMACMD5Hash 将字符串用HMAC-MD5编码后返回

func HMACSHA1Hash added in v0.1.5

func HMACSHA1Hash(text string, key string) string

HMACSHA1Hash 将字符串用HMAC-SHA1编码后返回

func HMACSHA224Hash added in v0.1.5

func HMACSHA224Hash(text string, key string) string

HMACSHA224Hash 将字符串用HMAC-SHA224编码后返回

func HMACSHA256Hash added in v0.1.5

func HMACSHA256Hash(text string, key string) string

HMACSHA256Hash 将字符串用HMAC-SHA256编码后返回

func HMACSHA384Hash added in v0.1.5

func HMACSHA384Hash(text string, key string) string

HMACSHA384Hash 将字符串用HMAC-SHA384编码后返回

func HMACSHA512Hash added in v0.1.5

func HMACSHA512Hash(text string, key string) string

HMACSHA512Hash 将字符串用HMAC-SHA512编码后返回

func HMACSHA512_224Hash added in v0.1.5

func HMACSHA512_224Hash(text string, key string) string

HMACSHA512_224Hash 将字符串用HMAC-SHA512/224编码后返回

func HMACSHA512_256Hash added in v0.1.5

func HMACSHA512_256Hash(text string, key string) string

HMACSHA512_256Hash 将字符串用HMAC-SHA512/256编码后返回

func HttpDelete added in v0.4.7

func HttpDelete(apiUrl string, params map[string]string, timeout time.Duration) (int, error)

HttpDelete 发送一个DELETE请求。 返回状态码和错误信息

func HttpGet added in v0.3.8

func HttpGet(apiUrl string, params map[string]string, timeout time.Duration) ([]byte, int, error)

HttpGet 发送一个GET请求。 返回响应体、状态码和错误信息

func HttpHead added in v0.3.8

func HttpHead(apiUrl string, params map[string]string, timeout time.Duration) (int, error)

HttpHead 发送一个Head请求。 返回状态码和错误信息

func HttpOptions added in v0.4.7

func HttpOptions(apiUrl string, timeout time.Duration) (http.Header, int, error)

HttpOptions 发送一个OPTIONS请求。 返回响应头、状态码和错误信息

func HttpPatch added in v0.4.7

func HttpPatch(apiUrl string, params map[string]string, body any, timeout time.Duration) ([]byte, int, error)

HttpPatch 发送一个带有JSON格式body的PATCH请求。 返回响应体、状态码和错误信息

func HttpPost added in v0.3.8

func HttpPost(apiUrl string, params map[string]string, body any, timeout time.Duration) ([]byte, int, error)

HttpPost 发送一个带有JSON格式body的POST请求。 返回响应体、状态码和错误信息

func HttpPut added in v0.4.7

func HttpPut(apiUrl string, params map[string]string, body any, timeout time.Duration) ([]byte, int, error)

HttpPut 发送一个带有JSON格式body的PUT请求。 返回响应体、状态码和错误信息

func HttpTrace added in v0.4.7

func HttpTrace(apiUrl string, timeout time.Duration) ([]byte, int, error)

HttpTrace 发送一个TRACE请求。 返回响应体、状态码和错误信息

func IpToUint32 added in v0.3.9

func IpToUint32(ipStr string) (uint32, error)

IpToUint32 将IP地址转换为整数 传入 192.168.1.1,返回 3232235777

func IsIPInCIDR added in v0.5.7

func IsIPInCIDR(ip string, cidr string) bool

IsIPInCIDR 判断IP地址是否在CIDR网段内 传入 192.168.1.1 和 192.168.0.0/16,返回 true

func IsValidIP added in v0.1.1

func IsValidIP(ipAddr string) bool

IsValidIP 检测IP地址是否合法

func IsValidPort added in v0.1.1

func IsValidPort(port string) (int, error)

IsValidPort 检测端口是否合法

func IsValidURL added in v0.1.1

func IsValidURL(u string) bool

IsValidURL 检测给定字符串是否是一个合法的URL。

func MD5Hash added in v0.1.5

func MD5Hash(text string, num int) string

MD5Hash 将字符串用MD5编码后返回

func MD5HashWithSalt added in v0.1.5

func MD5HashWithSalt(text, salt string, num int) string

MD5HashWithSalt 将字符串用MD5编码后返回,并可以添加盐值

func MatchString added in v0.1.7

func MatchString(src, pattern string) bool

MatchString 检查正则表达式是否与给定的源字符串匹配。

func NowTimeMillisecond added in v0.1.6

func NowTimeMillisecond() string

NowTimeMillisecond 获取当前时间,格式为:2006-01-02 15:04:05.0000

func NowTimeSecond added in v0.1.6

func NowTimeSecond() string

NowTimeSecond 获取当前时间,格式为:2006-01-02 15:04:05

func Ping added in v0.1.1

func Ping(ip string, timeout time.Duration) (bool, error)

Ping 使用 ICMP 发送回显请求并等待回显回复(只支持ipv4)

func PingUseCommand added in v0.7.4

func PingUseCommand(ipAddr string) (bool, error)

PingUseCommand 使用系统ping命令 对目的地址发起ping检测icmp是否通畅(只支持ipv4)

func RemoveFirstString added in v0.1.8

func RemoveFirstString(slice []string) []string

RemoveFirstString 从字符串数组切片里移除第一个元素

func RemoveItemsContainingSubstring added in v0.1.1

func RemoveItemsContainingSubstring(arr []string, substr string) []string

RemoveItemsContainingSubstring 从字符串数组切片里移除包含substr字符串的项并移除连续空格字符

func RemoveItemsNotContainingSubstring added in v0.1.1

func RemoveItemsNotContainingSubstring(arr []string, substr string) []string

RemoveItemsNotContainingSubstring 从字符串数组切片里移除不包含substr字符串的项并移除连续空格字符

func RemoveMatchString added in v0.1.9

func RemoveMatchString(slice []string, pattern string, method int) []string

RemoveMatchString 按正则表达式移除字符串数组切片中的元素 method为1则移除匹配项 method为2则移除不匹配项

func RemoveMultiSpaces added in v0.1.1

func RemoveMultiSpaces(str string) string

RemoveMultiSpaces 从字符串中移除多个连续空格字符

func RemoveTextWithinMatch added in v0.2.0

func RemoveTextWithinMatch(str string, pattern string, method int) string

RemoveTextWithinMatch 按正则表达式移除字符串中的行 method为1则移除匹配项 method为2则移除不匹配项

func RemoveTextWithinParens added in v0.1.1

func RemoveTextWithinParens(str string) string

RemoveTextWithinParens 从字符串中删除括号内的文本。

func SHA1Hash added in v0.1.5

func SHA1Hash(text string) string

SHA1Hash 将字符串用SHA1编码后返回

func SHA224Hash added in v0.1.5

func SHA224Hash(text string) string

SHA224Hash 将字符串用SHA224编码后返回

func SHA256Hash added in v0.1.5

func SHA256Hash(text string) string

SHA256Hash 将字符串用SHA256编码后返回

func SHA384Hash added in v0.1.5

func SHA384Hash(text string) string

SHA384Hash 将字符串用SHA384编码后返回

func SHA512Hash added in v0.1.5

func SHA512Hash(text string) string

SHA512Hash 将字符串用SHA512编码后返回

func SHA512_224Hash added in v0.1.5

func SHA512_224Hash(text string) string

SHA512_224Hash 将字符串用SHA512/224编码后返回

func SHA512_256Hash added in v0.1.5

func SHA512_256Hash(text string) string

SHA512_256Hash 将字符串用SHA512/256编码后返回

func SecondsToHuman added in v0.5.3

func SecondsToHuman(input int64) string

SecondsToHuman 将秒转换为天、小时、分钟和秒的字符串

func SortStructs added in v0.3.3

func SortStructs[T any](slice []T, fieldName string, ascending bool) error

SortStructs 对结构体切片进行排序 T为结构体

func StringEndWith added in v0.1.7

func StringEndWith(str, suffix string) bool

StringEndWith 检查字符串是否以指定的后缀结尾。

func StringEndWithIgnoreCase added in v0.1.7

func StringEndWithIgnoreCase(str, suffix string) bool

StringEndWithIgnoreCase 检查字符串是否以指定的后缀结尾,忽略大小写。

func StringIsEmpty added in v0.1.7

func StringIsEmpty(str string) bool

StringIsEmpty 检查字符串是否为空。

func StringIsInt added in v0.1.6

func StringIsInt(str string) (int, bool)

StringIsInt 检查字符串是否是一个整数。

func StringIsInt64 added in v0.1.6

func StringIsInt64(str string) (int64, bool)

StringIsInt64 检查字符串是否是一个64位整数。

func StringStartWith added in v0.1.7

func StringStartWith(str, prefix string) bool

StringStartWith 检查字符串是否以指定的前缀开头。

func StringStartWithIgnoreCase added in v0.1.7

func StringStartWithIgnoreCase(str, prefix string) bool

StringStartWithIgnoreCase 检查字符串是否以指定的前缀开头,忽略大小写。

func StringToLocalTime added in v0.2.5

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

StringToLocalTime 字符串转换为本地时间

func SubNetMaskToBits added in v0.1.8

func SubNetMaskToBits(subnetMask string) (int, error)

SubNetMaskToBits 将子网掩码转换为掩码位数 传入 255.255.255.0,返回 24

func TimeToTimestamp added in v0.2.2

func TimeToTimestamp(t time.Time, precision int) int64

TimeToTimestamp 根据指定的精度将 time.Time 转换为时间戳

func TimestampToTime added in v0.2.2

func TimestampToTime(ts int64, precision int) time.Time

TimestampToTime 根据指定的精度将时间戳转换回 time.Time

func TrimLeadingCharacter added in v0.1.7

func TrimLeadingCharacter(str, character string) string

TrimLeadingCharacter 从字符串中删除前导字符。

func TrimLeadingCharacterIgnoreCase added in v0.1.7

func TrimLeadingCharacterIgnoreCase(str, character string) string

TrimLeadingCharacterIgnoreCase 从字符串中删除前导字符,忽略大小写。

func TrimTrailingCharacter added in v0.1.7

func TrimTrailingCharacter(str, character string) string

TrimTrailingCharacter 从字符串中删除尾随字符。

func TrimTrailingCharacterIgnoreCase added in v0.1.7

func TrimTrailingCharacterIgnoreCase(str, character string) string

TrimTrailingCharacterIgnoreCase 从字符串中删除尾随字符,忽略大小写。

func Uint32ToIP added in v0.3.9

func Uint32ToIP(ipInt uint32) string

Uint32ToIP 将整数转换为IP地址 传入 3232235777,返回 192.168.1.1

func ValidateAndConvertCIDR added in v0.5.4

func ValidateAndConvertCIDR(cidr string) uint32

ValidateAndConvertCIDR 验证CIDR是否合法,并转换网络地址为uint32 传入 192.168.1.1/24,返回 3232235776

Types

type SlicePagination added in v0.3.4

type SlicePagination[T any] struct {
	Page        int  // 当前页
	PageSize    int  // 每页条数
	Total       int  // 总条数
	TotalPages  int  // 总页数
	Items       []T  // 当前页的数据
	HasPrevPage bool // 是否有上一页
	HasNextPage bool // 是否有下一页
}

SlicePagination 表示包含所有必要元数据的分页结果。

func PaginateSlice added in v0.3.4

func PaginateSlice[T any](items []T, page, pageSize int) SlicePagination[T]

PaginateSlice 获取任何类型的切片、页码和页面大小,并返回 SlicePagination.

Jump to

Keyboard shortcuts

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