import "github.com/Akvicor/util"- CRC32
- MD5 (File, reader, string, bytes)
- SHA1 (File, reader, string, bytes)
- SHA256 (File, reader, string, bytes)
- SHA512 (File, reader, string, bytes)
- AES (CBC)
- Base64
- bytes to int (uint16, uint32, uint64)
- int to bytes (uint16, uint32, uint64)
- Bytes Combine
- Bit Set
- JSON
- Slice Remove Duplicates
- File Stat
- Dir List
- Mkdir
- Dir Size
- Size Unit, Decimal System and Binary System
- Path Split
- Random String, Key
- IP
- Time Calculate
- Tcp Port Checker
- Port
- Get Client IP & HTTP GET/POST/PUT Request & HTTP Response & Redirect
各类型提供的函数方法名称类似,只需要将函数名中的CRC32替换为其他类型名即可使用
以下以CRC32为例子
- 计算结果为
*CRC32Result类型,可通过以下方法获取不同类型结果 .Value() uint32CRC32独有.Array() [4]byte各类型长度不一致.Slice() []byte.Upper() string.Lower() string.Error() error获取计算过程中产生的错误
// 示例
fmt.Println(NewCRC32().FromString("Akvicor").Lower()).FromReader()从Reader中读取数据并计算.FromFile()从文件中读取数据并计算.FromReaderChunk()从Reader中分块读取数据并计算.FromFileChunk()从文件中分块读取数据并计算.FromBytes()计算byte数组.FromString()计算字符串
// 示例
cp := NewCRC32Pip()
io.WriteString(cp, "Akvicor")
fmt.Println(cp.Result().Value()).Write()Writer实现.WriteBytes()写入[]byte.WriteString()写入string.Result()获取计算结果
- 计算结果为
*AESResult类型,可通过以下方法获取不同类型结果 .Bytes() []byte.Base64() *Base64Result.String() string.Upper() string.Lower() string.Encrypted() bool.Decrypt(key []byte, iv ...[]byte) *AESResult.Encrypt(key []byte, iv ...[]byte) *AESResult.Error() error获取计算过程中产生的错误
.EncryptCBC(origData, key []byte, iv ...[]byte) *AESResult.DecryptCBC(encrypted, key []byte, iv ...[]byte) *AESResult
- 计算结果为
*Base64Result类型,可通过以下方法获取不同类型结果 .Bytes() []byte.String() string.Error() error获取计算过程中产生的错误
.EncodeBytes()编码[]byte.EncodeString()编码string.DecodeBytes()解码[]byte.DecodeString()解码string
支持16/32/64位无符号整型,以下仅列出16位相关函数
.UInt16ToBytesSlice()uint16转[]byte.BytesSliceToUInt16()[]byte转uint16.UInt16ToBytesArray()uint16转[...]byte.BytesArrayToUInt16()[...]byte转uint16
合并多个[]byte
b := BytesCombine([]byte{0x11, 0x22}, []byte{0x33, 0x44, 0x55}, []byte{0x66})设置整型变量二进制位,支持byte,uint8,int8,uint16,int16,uint32,int32,uint64,int64
b8 := uint8(0)
BitSet(&b8, 0, true)type TestStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
test1 := TestStruct{
Name: "Akvicor",
Age: 17,
}
fmt.Println(NewJSON(&test1, false).String())
fmt.Println(NewJSONResult([]byte(`{"name":"Akvicor","age":17}`)).Map())NewJSONResult(data []byte) *JSON传入json格式数据并储存,自动判断是json对象还是json数组NewJSON(v any, isArray bool) *JSON将传入变量转为json并储存,通过isArray指名传入的是否是数组- 计算结果为
*JSON类型,可通过以下方法获取不同类型结果 .Bytes() []byte.String() string.Map(idx ...int) map[string]any返回json对象,如果变量中保存的是json数组,返回idx位置的对象,默认为0.MapArray() []map[string]any返回json数组,如果变量中保存的是json对象,自动创建一个数组并将变量作为数组第一个元素.Error() error获取计算过程中产生的错误
Slice去除重复元素
NewRemoveDuplicates() *RemoveDuplicates.String(s []string) []string.Byte(s []byte) []byte.Int8(s []int8) []int8.Int16(s []int16) []int16.Int(s []int) []int.Int32(s []int32) []int32.Int64(s []int64) []int64.UInt8(s []uint8) []uint8.UInt16(s []uint16) []uint16.UInt(s []uint) []uint.UInt32(s []uint32) []uint32.UInt64(s []uint64) []uint64.Float32(s []float32) []float32.Float64(s []float64) []float64
判断文件类型(不存在,是文件夹,是文件
FileStat(filename string) *FileStatModel通过以下函数确定文件类型
IsFile()
NotFile()
IsDir()
NotDir()
IsExist()
NotExist()
IsDenied()
NotDenied()
IsError()
NotError()获取文件列表
DirList(p string) *DirListModel
type DirListModel struct {
Files []DirListUnitModel
Dirs []DirListUnitModel
Error error
}
type DirListUnitModel struct {
Name string
Size int64
Mode fs.FileMode
ModTime time.Time
}创建文件夹
MkdirP(p string, perm ...os.FileMode) error获取文件夹大小
DirSize(p string) (int64, error)将大小单位B转换为K,M,G,T,P,E,并支持返回格式化后的字符串
- 以
KiB,MiB,GiB,TiB,PiB,EiB为单位(1024进制 - 以
KB,MB,GB,TB,PB,EB为单位(1000进制 - 以
K,M,G,T,P,E为单位(转换为iB或B时,按照1024进制处理 - 将B单位的值格式化到各个单位(如
1025B返回1KiB 1B,返回string或[]*SizeFormatModel - 截取最大的单位的大小(如1M2B返回1M
Size(1*SizeB + 2*SizeKiB).Format(",", true)
Size(1*SizeB + 2*SizeKB).Format(",", true)三种方法分割路径字符串
SplitPathSkip(p string, skip int) (head, tail string)跳过skip个/后,在下一个/处分割,且/保留在tail中SplitPath(p string) (head, tail string)在第一个/处分割,且/保留在tail中SplitPathRepeat(p string, repeat int) (head, tail string)在第一个/处分割,且/保留在tail中,将tail作为参数再次分割,重复repeat次
返回随机字符串
字符
RandomLower小写字母RandomUpper大写字母RandomDigit数字RandomSpecial特殊字符RandomAlpha字母RandomAll各类字符拼接成的单个字符串RandomSlice各类型字符组成的字符串数组
方法
RandomString(length int, str ...string)随机生成,默认通过RandomAll生成,也可通过传入str来自定义RandomStringAtLeastOnce(length int, str ...string)每种类型至少包含一个,每个str元素为一个类型,默认通过RandomSlice生成,也可通过传入str来自定义RandomStringWithTimestamp(length int, unix ...int64)长度至少为8,返回包含时间戳的随机字符串ParseRandomStringWithTimestamp(str string) (int64, string)解析包含时间戳的随机字符串RandomKey(l int) *KeyResult生成一个长度为l的key的byte数组, 返回包含key的结构体KeyResultFromHexString(hx string) *KeyResult解析hex字符串,转换为KeyResult
KeyResult下的方法
.Bytes返回key的byte数组.Upper返回hex后的大写字符串,长度为Bytes的两倍.Lower返回hex后的小写字符串,长度为Bytes的两倍.Error
IsIPAddr判断是否是ip地址IsLocalIPAddr判断ip地址是否是本地ipIsLocalIP判断ip是否是本地ipIPAddrToUint32ip地址转为uint32Uint32ToIPAddruint32转为ip地址IPToUint32ip转为uint32Uint32ToIPuint32转为ipGetLocalIp获取本地IP
TimeNowFormat返回当前时间的格式化字符串TimeNowToBase36返回当前时间的36进制字符串TimeUnixToFormatunix转为格式化字符串TimeUnixToBase36unix转为36进制字符串TimeBase36ToUnix36进制字符串转为unixYearBetweenTwoDate计算两个日期年份相减,不计算月等等YearBetweenTwoTime计算两个日期间隔的年份,计算到秒MonthBetweenTwoDate计算两个日期相差月份,不计算日等等MonthBetweenTwoTime计算两个日期相差月份,计算到秒DayBetweenTwoDate计算两个日期相差天数,不计算小时等等DayBetweenTwoTime计算两个日期相差天数,计算到秒HourBetweenTwoTime计算两个日期相差小时MinuteBetweenTwoTime计算两个日期相差分钟SecondBetweenTwoTime计算两个日期相差秒
检测端口是否开放
TcpPortIsOpen(ip, port string)TcpPortIsOpenByAddr(ipPort string)
获取可用端口
GetAvailablePort() (int, error)
RemoteIP通过RemoteAddr获取ipGetClientIP获取客户端ip,包含内网地址GetClientPublicIP获取客户端ip
RespRedirect重定向LastPage上一个页面Reload刷新页面
NewHTTPResp创建响应数据结构体变量ParseHTTPResp解析json字符串为响应数据结构体变量WriteHTTPRespAPIOk返回 okWriteHTTPRespAPIFailed返回 failedWriteHTTPRespAPIInvalidKey返回 非法的keyWriteHTTPRespAPIInvalidInput返回 非法的输入WriteHTTPRespAPIProcessingFailed返回 处理失败
Content Type
HTTPContentTypeUrlencodedHTTPContentTypeUrlencodedUTF8HTTPContentTypeJsonHTTPContentTypeJsonUTF8HTTPContentTypeXmlHTTPContentTypeXmlUTF8HTTPContentTypePlainHTTPContentTypePlainUTF8HTTPContentTypeHtmlHTTPContentTypeHtmlUTF8HTTPContentTypeFormDataHTTPContentTypeFormDataUTF8
方法
HttpGet(u string, args any)u为url,args为可序列化变量HttpPost(contentType string, u string, args any)HttpPostGet(contentType string, u string, argsGET, argsPOST any)