util

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 23 Imported by: 1

README

Util

import "git.viry.cc/gomod/util"
  • CRC32
  • MD5 (File, reader, string, bytes)
  • SHA1 (File, reader, string, bytes)
  • SHA256 (File, reader, string, bytes)
  • SHA512 (File, reader, string, bytes)
  • Base64
  • bytes to int (uint16, uint32, uint64)
  • int to bytes (uint16, uint32, uint64)
  • Bytes Combine
  • Bit Set
  • JSON
  • File Stat
  • Path Split
  • Random String
  • IP
  • Time Calculate
  • Tcp Port Checker
  • Get Client IP & HTTP GET/POST/PUT Request & HTTP Response & Redirect

CRC32 & MD5 & SHA1 & SHA256 & SHA512

各类型提供的函数方法名称类似,只需要将函数名中的CRC32替换为其他类型名即可使用

以下以CRC32为例子

  • 计算结果为*CRC32Result类型,可通过以下方法获取不同类型结果
  • .Value() uint32 CRC32独有
  • .Array() [4]byte 各类型长度不一致
  • .Slice() []byte
  • .Upper() string
  • .Lower() string
  • .Error() error 获取计算过程中产生的错误

NewCRC32()

// 示例
fmt.Println(NewCRC32().FromString("Akvicor").Lower())
  • .FromReader() 从Reader中读取数据并计算
  • .FromFile() 从文件中读取数据并计算
  • .FromReaderChunk() 从Reader中分块读取数据并计算
  • .FromFileChunk() 从文件中分块读取数据并计算
  • .FromBytes() 计算byte数组
  • .FromString() 计算字符串

NewCRC32Pip()

// 示例
cp := NewCRC32Pip()
io.WriteString(cp, "Akvicor")
fmt.Println(cp.Result().Value())
  • .Write() Writer实现
  • .WriteBytes() 写入[]byte
  • .WriteString() 写入string
  • .Result() 获取计算结果

Base64

  • 计算结果为*Base64Result类型,可通过以下方法获取不同类型结果
  • .Bytes() []byte
  • .String() string
  • .Error() error 获取计算过程中产生的错误

NewBase64()

  • .EncodeBytes() 编码[]byte
  • .EncodeString() 编码string
  • .DecodeBytes() 解码[]byte
  • .DecodeString() 解码string

[]byte <-> uint

支持16/32/64位无符号整型,以下仅列出16位相关函数

  • .UInt16ToBytesSlice() uint16[]byte
  • .BytesSliceToUInt16() []byteuint16
  • .UInt16ToBytesArray() uint16[...]byte
  • .BytesArrayToUInt16() [...]byteuint16

Bytes Combine

合并多个[]byte

b := BytesCombine([]byte{0x11, 0x22}, []byte{0x33, 0x44, 0x55}, []byte{0x66})

Bit Set

设置整型变量二进制位,支持byte,uint8,int8,uint16,int16,uint32,int32,uint64,int64

b8 := uint8(0)
BitSet(&b8, 0, true)

JSON

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 获取计算过程中产生的错误

File Stat

判断文件类型(不存在,是文件夹,是文件

FileStat(filename string) int

通过以下常量和返回值确定文件类型

FileStatNotExist = 0
FileStatIsDir    = 1
FileStatIsFile   = 2

Path Split

三种方法分割路径字符串

  • 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次

Random String

返回随机字符串

字符

  • 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) 解析包含时间戳的随机字符串

IP

  • IsIPAddr 判断是否是ip地址
  • IsLocalIPAddr 判断ip地址是否是本地ip
  • IsLocalIP 判断ip是否是本地ip
  • IPAddrToUint32 ip地址转为uint32
  • Uint32ToIPAddr uint32转为ip地址
  • IPToUint32 ip转为uint32
  • Uint32ToIP uint32转为ip

Time Calculate

  • TimeNowFormat 返回当前时间的格式化字符串
  • TimeNowToBase36 返回当前时间的36进制字符串
  • TimeUnixToFormat unix转为格式化字符串
  • TimeUnixToBase36 unix转为36进制字符串
  • TimeBase36ToUnix 36进制字符串转为unix
  • YearBetweenTwoDate 计算两个日期年份相减,不计算月等等
  • YearBetweenTwoTime 计算两个日期间隔的年份,计算到秒
  • MonthBetweenTwoDate 计算两个日期相差月份,不计算日等等
  • MonthBetweenTwoTime 计算两个日期相差月份,计算到秒
  • DayBetweenTwoDate 计算两个日期相差天数,不计算小时等等
  • DayBetweenTwoTime 计算两个日期相差天数,计算到秒
  • HourBetweenTwoTime 计算两个日期相差小时
  • MinuteBetweenTwoTime 计算两个日期相差分钟
  • SecondBetweenTwoTime 计算两个日期相差秒

Tcp Port Checker

检测端口是否开放

  • TcpPortIsOpen(ip, port string)
  • TcpPortIsOpenByAddr(ipPort string)

Get Client IP & HTTP GET/POST/PUT Request & HTTP Response & Redirect

获取IP

  • RemoteIP 通过RemoteAddr获取ip
  • GetClientIP 获取客户端ip,包含内网地址
  • GetClientPublicIP 获取客户端ip

页面跳转

  • RespRedirect 重定向
  • LastPage 上一个页面
  • Reload 刷新页面

API响应

  • NewHTTPResp 创建响应数据结构体变量
  • ParseHTTPResp 解析json字符串为响应数据结构体变量
  • WriteHTTPRespAPIOk 返回 ok
  • WriteHTTPRespAPIFailed 返回 failed
  • WriteHTTPRespAPIInvalidKey 返回 非法的key
  • WriteHTTPRespAPIInvalidInput 返回 非法的输入
  • WriteHTTPRespAPIProcessingFailed 返回 处理失败

GET & POST

Content Type

  • HTTPContentTypeUrlencoded
  • HTTPContentTypeJson

方法

  • HttpGet(u string, args any) u为url,args为可序列化变量
  • HttpPost(contentType string, u string, args any)
  • HttpPostGet(contentType string, u string, argsGET, argsPOST any)

Documentation

Index

Examples

Constants

View Source
const (
	FileStatNotExist = 0
	FileStatIsDir    = 1
	FileStatIsFile   = 2
)
View Source
const (
	HTTPRespCodeOKCode               HTTPRespCode = 0
	HTTPRespCodeOKMsg                string       = "ok"
	HTTPRespCodeERCode               HTTPRespCode = 1
	HTTPRespCodeERMsg                string       = "failed to respond"
	HTTPRespCodeInvalidKeyCode       HTTPRespCode = 2
	HTTPRespCodeInvalidKeyMsg        string       = "invalid key"
	HTTPRespCodeProcessingFailedCode HTTPRespCode = 3
	HTTPRespCodeProcessingFailedMsg  string       = "processing failed"
	HTTPRespCodeInvalidInputCode     HTTPRespCode = 4
	HTTPRespCodeInvalidInputMsg      string       = "invalid input"
)
View Source
const (
	HTTPContentTypeUrlencoded = "application/x-www-form-urlencoded; charset=utf-8"
	HTTPContentTypeJson       = "application/json; charset=UTF-8"
)
View Source
const MD5ResultLength = 16
View Source
const RandomAlpha = RandomLower + RandomUpper
View Source
const RandomDigit = "0123456789"
View Source
const RandomLower = "abcdefghijklmnopqrstuvwxyz"
View Source
const RandomSpecial = "!_.~?-+=#@$%"
View Source
const RandomStringWithTimestampTimeLength = 8
View Source
const RandomUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
View Source
const SHA1ResultLength = 20
View Source
const SHA256ResultLength = 32
View Source
const SHA512ResultLength = 64
View Source
const TimeFormatLayout = "2006-01-02 15:04:05"

Variables

View Source
var ErrorInvalidIPV4Format = errors.New("invalid ipv4 format")

Functions

func BitSet

func BitSet(b interface{}, bit byte, set bool)
Example
v := 0
BitSet(&v, 1, true)
fmt.Println(v)
ui := uint8(0)
BitSet(&ui, 7, true)
fmt.Println(ui)
i := int8(0)
BitSet(&i, 7, true)
fmt.Println(i)
Output:

2
128
-128

func BytesArrayToUInt16 added in v1.7.1

func BytesArrayToUInt16(buf [2]byte) uint16
Example
bs := [2]byte{255, 255}
val := BytesArrayToUInt16(bs)
fmt.Println(val)

bs = [2]byte{127, 255}
val = BytesArrayToUInt16(bs)
fmt.Println(val)

bs = [2]byte{127, 254}
val = BytesArrayToUInt16(bs)
fmt.Println(val)
Output:

65535
32767
32766

func BytesArrayToUInt32 added in v1.7.1

func BytesArrayToUInt32(buf [4]byte) uint32
Example
bs := [4]byte{255, 255, 255, 255}
val := BytesArrayToUInt32(bs)
fmt.Println(val)

bs = [4]byte{127, 255, 255, 255}
val = BytesArrayToUInt32(bs)
fmt.Println(val)

bs = [4]byte{127, 254, 254, 254}
val = BytesArrayToUInt32(bs)
fmt.Println(val)
Output:

4294967295
2147483647
2147417854

func BytesArrayToUInt64 added in v1.7.1

func BytesArrayToUInt64(buf [8]byte) uint64
Example
bs := [8]byte{255, 255, 255, 255, 255, 255, 255, 255}
val := BytesArrayToUInt64(bs)
fmt.Println(val)

bs = [8]byte{127, 255, 255, 255, 255, 255, 255, 255}
val = BytesArrayToUInt64(bs)
fmt.Println(val)

bs = [8]byte{127, 254, 254, 254, 254, 254, 254, 254}
val = BytesArrayToUInt64(bs)
fmt.Println(val)
Output:

18446744073709551615
9223372036854775807
9223089458054627070

func BytesCombine

func BytesCombine(pBytes ...[]byte) []byte
Example
b1 := []byte{11, 22}
b2 := []byte{33, 44, 55}
b3 := []byte{66}

fmt.Println(BytesCombine(b1, b2, b3))
Output:

[11 22 33 44 55 66]

func BytesSliceToUInt16 added in v1.7.1

func BytesSliceToUInt16(buf []byte) uint16
Example
bs := []byte{255, 255}
val := BytesSliceToUInt16(bs)
fmt.Println(val)

bs = []byte{127, 255}
val = BytesSliceToUInt16(bs)
fmt.Println(val)

bs = []byte{127, 254}
val = BytesSliceToUInt16(bs)
fmt.Println(val)
Output:

65535
32767
32766

func BytesSliceToUInt32 added in v1.7.1

func BytesSliceToUInt32(buf []byte) uint32
Example
bs := []byte{255, 255, 255, 255}
val := BytesSliceToUInt32(bs)
fmt.Println(val)

bs = []byte{127, 255, 255, 255}
val = BytesSliceToUInt32(bs)
fmt.Println(val)

bs = []byte{127, 254, 254, 254}
val = BytesSliceToUInt32(bs)
fmt.Println(val)
Output:

4294967295
2147483647
2147417854

func BytesSliceToUInt64 added in v1.7.1

func BytesSliceToUInt64(buf []byte) uint64
Example
bs := []byte{255, 255, 255, 255, 255, 255, 255, 255}
val := BytesSliceToUInt64(bs)
fmt.Println(val)

bs = []byte{127, 255, 255, 255, 255, 255, 255, 255}
val = BytesSliceToUInt64(bs)
fmt.Println(val)

bs = []byte{127, 254, 254, 254, 254, 254, 254, 254}
val = BytesSliceToUInt64(bs)
fmt.Println(val)
Output:

18446744073709551615
9223372036854775807
9223089458054627070

func BytesToUInt

func BytesToUInt(buf []byte) uint64
Example
bs := []byte{255}
fmt.Println(BytesToUInt(bs))
bs = []byte{255, 255}
fmt.Println(BytesToUInt(bs))
bs = []byte{255, 255, 255, 255}
fmt.Println(BytesToUInt(bs))
bs = []byte{255, 255, 255, 255, 255, 255, 255, 255}
fmt.Println(BytesToUInt(bs))
bs = []byte{255, 255, 255, 255, 255, 255, 255}
fmt.Println(BytesToUInt(bs))
Output:

255
65535
4294967295
18446744073709551615
72057594037927935

func DayBetweenTwoDate added in v1.7.1

func DayBetweenTwoDate(t1, t2 time.Time) int64
Example
t1 := int64(1705628326)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(DayBetweenTwoDate(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(DayBetweenTwoDate(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(DayBetweenTwoDate(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2024-01-19 09:38:46 -> 2024-01-18 22:37:45
0
1
-1

func DayBetweenTwoTime added in v1.7.1

func DayBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1705628326)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(DayBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(DayBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(DayBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2024-01-19 09:38:46 -> 2024-01-18 22:37:45
0
0
0

func FileStat

func FileStat(filename string) int
Example
if FileStat("testfile") == FileStatIsDir {
	fmt.Println("FileStatIsDir")
}
if FileStat("testfile/testFile1") == FileStatIsFile {
	fmt.Println("FileStatIsFile")
}
if FileStat("testfile/testFile2") == FileStatNotExist {
	fmt.Println("FileStatNotExist")
}
Output:

FileStatIsDir
FileStatIsFile
FileStatNotExist

func GetClientIP added in v1.7.1

func GetClientIP(r *http.Request) string

func GetClientPublicIP added in v1.7.1

func GetClientPublicIP(r *http.Request) string

func HourBetweenTwoTime added in v1.7.1

func HourBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1705628326)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(HourBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(HourBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(HourBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2024-01-19 09:38:46 -> 2024-01-18 22:37:45
0
11
-11

func HttpGet added in v1.7.1

func HttpGet(u string, args any) []byte
Example
i := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts/1", nil))
i1 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 1}))
i2 := NewJSONResult(HttpGet("https://jsonplaceholder.typicode.com/posts", map[string]any{"id": 2}))
if fmt.Sprint(i.Map()["id"]) != "1" {
	fmt.Println(i)
}
if fmt.Sprint(i1.Map()["id"]) != "1" {
	fmt.Println(i1)
}
if fmt.Sprint(i2.MapArray()[0]["id"]) != "2" {
	fmt.Println(i2)
}
Output:

func HttpPost added in v1.7.1

func HttpPost(contentType string, u string, args any) []byte
Example
i1 := NewJSONResult(HttpPost(HTTPContentTypeUrlencoded, "https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t1", "body": "b1", "userId": 1}))
if fmt.Sprint(i1.Map()["id"]) != "101" {
	fmt.Println(i1)
}
Output:

func HttpPostGet added in v1.7.1

func HttpPostGet(contentType string, u string, argsGET, argsPOST any) []byte
Example
i1 := NewJSONResult(HttpPostGet(HTTPContentTypeUrlencoded, "https://jsonplaceholder.typicode.com/posts", map[string]any{"title": "t2", "body": "b2", "userId": 2}, map[string]any{"title": "t1", "body": "b1", "userId": 1}))
if fmt.Sprint(i1.Map()["id"]) != "101" {
	fmt.Println(i1)
}
Output:

func IPAddrToUint32 added in v1.7.1

func IPAddrToUint32(ip string) (uint32, error)

func IPToUint32 added in v1.7.1

func IPToUint32(ip net.IP) (uint32, error)

func IsIPAddr added in v1.7.1

func IsIPAddr(ip string) bool

func IsLocalIP added in v1.7.1

func IsLocalIP(ip net.IP) bool

func IsLocalIPAddr added in v1.7.1

func IsLocalIPAddr(ip string) bool

func LastPage added in v1.7.1

func LastPage(w http.ResponseWriter, r *http.Request)

func MinuteBetweenTwoTime added in v1.7.1

func MinuteBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1705628326)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MinuteBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(MinuteBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MinuteBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2024-01-19 09:38:46 -> 2024-01-18 22:37:45
0
661
-661

func MonthBetweenTwoDate added in v1.7.1

func MonthBetweenTwoDate(t1, t2 time.Time) int64
Example
t1 := int64(1739929126)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoDate(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(MonthBetweenTwoDate(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoDate(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211064)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoDate(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoDate(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211066)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoDate(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoDate(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2025-02-19 09:38:46 -> 2024-01-18 22:37:45
0
13
-13
2025-01-18 22:37:44 -> 2024-01-18 22:37:45
12
-12
2025-01-18 22:37:46 -> 2024-01-18 22:37:45
12
-12

func MonthBetweenTwoTime added in v1.7.1

func MonthBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1739929126)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(MonthBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211064)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211066)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(MonthBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(MonthBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2025-02-19 09:38:46 -> 2024-01-18 22:37:45
0
13
-13
2025-01-18 22:37:44 -> 2024-01-18 22:37:45
11
-11
2025-01-18 22:37:46 -> 2024-01-18 22:37:45
12
-12

func ParseRandomStringWithTimestamp

func ParseRandomStringWithTimestamp(str string) (int64, string)

func RandomString

func RandomString(length int, str ...string) string
Example
if RandomString(0, "a") != "" {
	fmt.Println("1 failed")
}
if RandomString(0, "abc") != "" {
	fmt.Println("2 failed")
}
if RandomString(1, "b") != "b" {
	fmt.Println("3 failed")
}
if RandomString(7, "a") != "aaaaaaa" {
	fmt.Println("4 failed")
}
if RandomString(0, "a", "b") != "" {
	fmt.Println("5 failed")
}
str := RandomString(7)
if len(str) != 7 {
	fmt.Println("6 failed")
}
Output:

func RandomStringAtLeastOnce added in v1.7.1

func RandomStringAtLeastOnce(length int, str ...string) string
Example
if RandomStringAtLeastOnce(0, "a") != "" {
	fmt.Println("1 failed")
}
if RandomStringAtLeastOnce(0, "abc") != "" {
	fmt.Println("2 failed")
}
if RandomStringAtLeastOnce(1, "b") != "b" {
	fmt.Println("3 failed")
}
if RandomStringAtLeastOnce(7, "a") != "aaaaaaa" {
	fmt.Println("4 failed")
}
if RandomStringAtLeastOnce(0, "a", "b") != "" {
	fmt.Println("5 failed")
}
str := RandomStringAtLeastOnce(2, "a", "b")
if str != "ab" && str != "ba" {
	fmt.Println("6 failed", str)
}
if RandomStringAtLeastOnce(1, "a", "b") != "a" {
	fmt.Println("7 failed")
}
str = RandomStringAtLeastOnce(7)
if len(str) != 7 {
	fmt.Println("8 failed")
}
Output:

func RandomStringWithTimestamp

func RandomStringWithTimestamp(length int, unix ...int64) string
Example
t := time.Now().Unix()
rstr := RandomStringWithTimestamp(17, t)
date, str := ParseRandomStringWithTimestamp(rstr)
if t != date || str != rstr[RandomStringWithTimestampTimeLength:] {
	fmt.Printf("before [%d] [%s] after [%d][%s]\n", t, rstr, date, str)
}
Output:

func Reload added in v1.7.1

func Reload(w http.ResponseWriter, r *http.Request)

func RemoteIP added in v1.7.1

func RemoteIP(r *http.Request) string

func RespRedirect added in v1.7.1

func RespRedirect(w http.ResponseWriter, r *http.Request, url string)

func SecondBetweenTwoTime added in v1.7.1

func SecondBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1705628326)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(SecondBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(SecondBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(SecondBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2024-01-19 09:38:46 -> 2024-01-18 22:37:45
0
39661
-39661

func SplitPath

func SplitPath(p string) (head, tail string)
Example
test := func(s string) {
	head, tail := SplitPath(s)
	fmt.Printf("s[%s] -> [%s, %s]\n", s, head, tail)
}
test("/123/akvicor")
test("123/akvicor")
test("/akvicor")
test("akvicor")
test("/")
test("")
Output:

s[/123/akvicor] -> [/123, /akvicor]
s[123/akvicor] -> [/123, /akvicor]
s[/akvicor] -> [/akvicor, ]
s[akvicor] -> [/akvicor, ]
s[/] -> [/, ]
s[] -> [/, ]

func SplitPathRepeat

func SplitPathRepeat(p string, repeat int) (head, tail string)
Example
test := func(s string, repeat int) {
	head, tail := SplitPathRepeat(s, repeat)
	fmt.Printf("s[%s] repeat[%d] -> [%s, %s]\n", s, repeat, head, tail)
}
s := "/1/23/456/7/"
test(s, 0)
test(s, 1)
test(s, 2)
test(s, 3)
test(s, 4)
test(s, 5)
s = ""
test(s, 0)
test(s, 1)
s = "/"
test(s, 0)
test(s, 1)
s = "url"
test(s, 0)
test(s, 1)
s = "/url"
test(s, 0)
test(s, 1)
s = "url/"
test(s, 0)
test(s, 1)
s = "/url/"
test(s, 0)
test(s, 1)
Output:

s[/1/23/456/7/] repeat[0] -> [/1, /23/456/7]
s[/1/23/456/7/] repeat[1] -> [/23, /456/7]
s[/1/23/456/7/] repeat[2] -> [/456, /7]
s[/1/23/456/7/] repeat[3] -> [/7, ]
s[/1/23/456/7/] repeat[4] -> [/, ]
s[/1/23/456/7/] repeat[5] -> [/, ]
s[] repeat[0] -> [/, ]
s[] repeat[1] -> [/, ]
s[/] repeat[0] -> [/, ]
s[/] repeat[1] -> [/, ]
s[url] repeat[0] -> [/url, ]
s[url] repeat[1] -> [/, ]
s[/url] repeat[0] -> [/url, ]
s[/url] repeat[1] -> [/, ]
s[url/] repeat[0] -> [/url, ]
s[url/] repeat[1] -> [/, ]
s[/url/] repeat[0] -> [/url, ]
s[/url/] repeat[1] -> [/, ]

func SplitPathSkip

func SplitPathSkip(p string, skip int) (head, tail string)
Example
test := func(s string, skip int) {
	head, tail := SplitPathSkip(s, skip)
	fmt.Printf("s[%s] skip[%d] -> [%s, %s]\n", s, skip, head, tail)
}
s := "/1/23/456/7/"
test(s, 0)
test(s, 1)
test(s, 2)
test(s, 3)
test(s, 4)
s = ""
test(s, 0)
test(s, 1)
s = "/"
test(s, 0)
test(s, 1)
s = "url"
test(s, 0)
test(s, 1)
s = "/url"
test(s, 0)
test(s, 1)
s = "url/"
test(s, 0)
test(s, 1)
s = "/url/"
test(s, 0)
test(s, 1)
Output:

s[/1/23/456/7/] skip[0] -> [/1, /23/456/7]
s[/1/23/456/7/] skip[1] -> [/1/23, /456/7]
s[/1/23/456/7/] skip[2] -> [/1/23/456, /7]
s[/1/23/456/7/] skip[3] -> [/1/23/456/7, ]
s[/1/23/456/7/] skip[4] -> [/1/23/456/7, ]
s[] skip[0] -> [/, ]
s[] skip[1] -> [/, ]
s[/] skip[0] -> [/, ]
s[/] skip[1] -> [/, ]
s[url] skip[0] -> [/url, ]
s[url] skip[1] -> [/url, ]
s[/url] skip[0] -> [/url, ]
s[/url] skip[1] -> [/url, ]
s[url/] skip[0] -> [/url, ]
s[url/] skip[1] -> [/url, ]
s[/url/] skip[0] -> [/url, ]
s[/url/] skip[1] -> [/url, ]

func TcpPortIsOpen added in v1.7.1

func TcpPortIsOpen(ip, port string) bool
Example
ip := "172.16.0.1"
fmt.Println(TcpPortIsOpen(ip, "22"))
fmt.Println(TcpPortIsOpen(ip, "80"))
fmt.Println(TcpPortIsOpen(ip, "443"))
fmt.Println(TcpPortIsOpen(ip, "444"))
Output:

true
true
true
false

func TcpPortIsOpenByAddr added in v1.7.1

func TcpPortIsOpenByAddr(ipPort string) bool
Example
fmt.Println(TcpPortIsOpenByAddr("172.16.0.1:22"))
fmt.Println(TcpPortIsOpenByAddr("172.16.0.1:80"))
fmt.Println(TcpPortIsOpenByAddr("172.16.0.1:443"))
fmt.Println(TcpPortIsOpenByAddr("172.16.0.1:444"))
Output:

true
true
true
false

func TimeBase36ToUnix added in v1.7.1

func TimeBase36ToUnix(t string) int64
Example
t := TimeBase36ToUnix("s7ijax")
fmt.Println(t)
t = TimeBase36ToUnix("00s7ijax")
fmt.Println(t)
Output:

1705675065
1705675065

func TimeNowFormat added in v1.7.1

func TimeNowFormat() string
Example
t := TimeNowFormat()
if len(t) != 19 {
	fmt.Println(t)
}
Output:

func TimeNowToBase36 added in v1.7.1

func TimeNowToBase36(length ...int) string
Example
s := TimeNowToBase36()
if len(s) < 1 {
	fmt.Println(s)
}
s = TimeNowToBase36(8)
if len(s) != 8 {
	fmt.Println(s)
}
Output:

func TimeUnixToBase36 added in v1.7.1

func TimeUnixToBase36(unix int64, length ...int) string
Example
s := TimeUnixToBase36(1705675065)
fmt.Println(s)
s = TimeUnixToBase36(1705675065, 8)
fmt.Println(s)
Output:

s7ijax
00s7ijax

func TimeUnixToFormat added in v1.7.1

func TimeUnixToFormat(unix int64) string
Example
t := TimeUnixToFormat(1705675065)
fmt.Println(t)
Output:

2024-01-19 22:37:45

func UInt16ToBytesArray added in v1.7.1

func UInt16ToBytesArray(i uint16) [2]byte
Example
val := uint16(65535)
bs := UInt16ToBytesArray(val)
fmt.Println(bs)

val /= 2
bs = UInt16ToBytesArray(val)
fmt.Println(bs)

val -= 1
bs = UInt16ToBytesArray(val)
fmt.Println(bs)
Output:

[255 255]
[127 255]
[127 254]

func UInt16ToBytesSlice added in v1.7.1

func UInt16ToBytesSlice(i uint16) []byte
Example
val := uint16(65535)
bs := UInt16ToBytesSlice(val)
fmt.Println(bs)

val /= 2
bs = UInt16ToBytesSlice(val)
fmt.Println(bs)

val -= 1
bs = UInt16ToBytesSlice(val)
fmt.Println(bs)
Output:

[255 255]
[127 255]
[127 254]

func UInt32ToBytesArray added in v1.7.1

func UInt32ToBytesArray(i uint32) [4]byte
Example
val := uint32(4294967295)
bs := UInt32ToBytesArray(val)
fmt.Println(bs)

val = uint32(2147483647)
bs = UInt32ToBytesArray(val)
fmt.Println(bs)

val = uint32(2147417854)
bs = UInt32ToBytesArray(val)
fmt.Println(bs)
Output:

[255 255 255 255]
[127 255 255 255]
[127 254 254 254]

func UInt32ToBytesSlice added in v1.7.1

func UInt32ToBytesSlice(i uint32) []byte
Example
val := uint32(4294967295)
bs := UInt32ToBytesSlice(val)
fmt.Println(bs)

val = uint32(2147483647)
bs = UInt32ToBytesSlice(val)
fmt.Println(bs)

val = uint32(2147417854)
bs = UInt32ToBytesSlice(val)
fmt.Println(bs)
Output:

[255 255 255 255]
[127 255 255 255]
[127 254 254 254]

func UInt64ToBytesArray added in v1.7.1

func UInt64ToBytesArray(i uint64) [8]byte
Example
val := uint64(18446744073709551615)
bs := UInt64ToBytesArray(val)
fmt.Println(bs)

val = uint64(9223372036854775807)
bs = UInt64ToBytesArray(val)
fmt.Println(bs)

val = uint64(9223089458054627070)
bs = UInt64ToBytesArray(val)
fmt.Println(bs)
Output:

[255 255 255 255 255 255 255 255]
[127 255 255 255 255 255 255 255]
[127 254 254 254 254 254 254 254]

func UInt64ToBytesSlice added in v1.7.1

func UInt64ToBytesSlice(i uint64) []byte
Example
val := uint64(18446744073709551615)
bs := UInt64ToBytesSlice(val)
fmt.Println(bs)

val = uint64(9223372036854775807)
bs = UInt64ToBytesSlice(val)
fmt.Println(bs)

val = uint64(9223089458054627070)
bs = UInt64ToBytesSlice(val)
fmt.Println(bs)
Output:

[255 255 255 255 255 255 255 255]
[127 255 255 255 255 255 255 255]
[127 254 254 254 254 254 254 254]

func UIntToBytes

func UIntToBytes(i interface{}) []byte
Example
val8 := uint8(254)
bs := UIntToBytes(val8)
fmt.Println(bs)

val16 := uint16(65534)
bs = UIntToBytes(val16)
fmt.Println(bs)

val32 := uint32(4294967294)
bs = UIntToBytes(val32)
fmt.Println(bs)

val64 := uint64(18446744073709551614)
bs = UIntToBytes(val64)
fmt.Println(bs)

valErr := ""
bs = UIntToBytes(valErr)
fmt.Println(bs)
Output:

[254]
[255 254]
[255 255 255 254]
[255 255 255 255 255 255 255 254]
[]

func Uint32ToIP added in v1.7.1

func Uint32ToIP(i uint32) net.IP

func Uint32ToIPAddr added in v1.7.1

func Uint32ToIPAddr(i uint32) string

func WriteHTTPRespAPIFailed added in v1.7.1

func WriteHTTPRespAPIFailed(w http.ResponseWriter, msg ...any)

func WriteHTTPRespAPIInvalidInput added in v1.7.1

func WriteHTTPRespAPIInvalidInput(w http.ResponseWriter, msg ...any)

func WriteHTTPRespAPIInvalidKey added in v1.7.1

func WriteHTTPRespAPIInvalidKey(w http.ResponseWriter, msg ...any)

func WriteHTTPRespAPIOk added in v1.7.1

func WriteHTTPRespAPIOk(w http.ResponseWriter, msg ...any)

func WriteHTTPRespAPIProcessingFailed added in v1.7.1

func WriteHTTPRespAPIProcessingFailed(w http.ResponseWriter, msg ...any)

func YearBetweenTwoDate added in v1.7.1

func YearBetweenTwoDate(t1, t2 time.Time) int64
Example
t1 := int64(1739929126)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(YearBetweenTwoDate(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(YearBetweenTwoDate(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(YearBetweenTwoDate(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2025-02-19 09:38:46 -> 2024-01-18 22:37:45
0
1
-1

func YearBetweenTwoTime added in v1.7.1

func YearBetweenTwoTime(t1, t2 time.Time) int64
Example
t1 := int64(1739929126)
t2 := int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(YearBetweenTwoTime(time.Unix(t1, 0), time.Unix(t1, 0)))
fmt.Println(YearBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(YearBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211064)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(YearBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(YearBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
t1 = int64(1737211066)
t2 = int64(1705588665)
fmt.Printf("%s -> %s\n", TimeUnixToFormat(t1), TimeUnixToFormat(t2))
fmt.Println(YearBetweenTwoTime(time.Unix(t1, 0), time.Unix(t2, 0)))
fmt.Println(YearBetweenTwoTime(time.Unix(t2, 0), time.Unix(t1, 0)))
Output:

2025-02-19 09:38:46 -> 2024-01-18 22:37:45
0
1
-1
2025-01-18 22:37:44 -> 2024-01-18 22:37:45
0
0
2025-01-18 22:37:46 -> 2024-01-18 22:37:45
1
-1

Types

type Base64 added in v1.7.1

type Base64 struct{}

func NewBase64 added in v1.7.1

func NewBase64() *Base64
Example
testString := "Akvicor"
testStringBase64 := "QWt2aWNvcg=="

encodeS := NewBase64().EncodeString(testString)
fmt.Println(encodeS)
encodeB := NewBase64().EncodeBytes([]byte(testString))
fmt.Println(encodeB.String())
decodeS := NewBase64().DecodeString(testStringBase64)
if decodeS.Error() == nil {
	fmt.Println(decodeS)
}
decodeB := NewBase64().DecodeBytes([]byte(testStringBase64))
fmt.Println(decodeB.Bytes())
Output:

QWt2aWNvcg==
QWt2aWNvcg==
Akvicor
[65 107 118 105 99 111 114]

func (*Base64) DecodeBytes added in v1.7.1

func (b *Base64) DecodeBytes(data []byte) *Base64Result

func (*Base64) DecodeString added in v1.7.1

func (b *Base64) DecodeString(str string) *Base64Result

func (*Base64) EncodeBytes added in v1.7.1

func (b *Base64) EncodeBytes(data []byte) *Base64Result

func (*Base64) EncodeString added in v1.7.1

func (b *Base64) EncodeString(str string) *Base64Result

type Base64Result added in v1.7.1

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

func NewBase64Result added in v1.7.1

func NewBase64Result(result string, err error) *Base64Result

func (*Base64Result) Bytes added in v1.7.1

func (b *Base64Result) Bytes() []byte

func (*Base64Result) Error added in v1.7.1

func (b *Base64Result) Error() error

func (*Base64Result) String added in v1.7.1

func (b *Base64Result) String() string

type CRC32 added in v1.7.1

type CRC32 struct{}

func NewCRC32 added in v1.7.1

func NewCRC32() *CRC32
Example
crc := NewCRC32()
res := crc.FromString("Akvicor")
if res.Error() == nil {
	fmt.Println(res.Value())
	fmt.Println(res.Lower())
	fmt.Println(res.Upper())
}
Output:

996205005
3b60e1cd
3B60E1CD

func (*CRC32) FromBytes added in v1.7.1

func (c *CRC32) FromBytes(data []byte) *CRC32Result

func (*CRC32) FromFile added in v1.7.1

func (c *CRC32) FromFile(filepath string) *CRC32Result

func (*CRC32) FromFileChunk added in v1.7.1

func (c *CRC32) FromFileChunk(filepath string, chunksize int) *CRC32Result

func (*CRC32) FromReader added in v1.7.1

func (c *CRC32) FromReader(r io.Reader) *CRC32Result

func (*CRC32) FromReaderChunk added in v1.7.1

func (c *CRC32) FromReaderChunk(r io.Reader, chunksize int) *CRC32Result

func (*CRC32) FromString added in v1.7.1

func (c *CRC32) FromString(data string) *CRC32Result

type CRC32Pip added in v1.7.1

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

func NewCRC32Pip added in v1.7.1

func NewCRC32Pip() *CRC32Pip
Example
cp := NewCRC32Pip()
io.WriteString(cp, "Akvicor")
res := cp.Result()
if res.Error() == nil {
	fmt.Println(res.Array())
	fmt.Println(res.Slice())
}
Output:

[59 96 225 205]
[59 96 225 205]

func (*CRC32Pip) Result added in v1.7.1

func (c *CRC32Pip) Result() *CRC32Result

func (*CRC32Pip) Write added in v1.7.1

func (c *CRC32Pip) Write(data []byte) (n int, err error)

func (*CRC32Pip) WriteBytes added in v1.7.1

func (c *CRC32Pip) WriteBytes(data []byte)

func (*CRC32Pip) WriteString added in v1.7.1

func (c *CRC32Pip) WriteString(data string)

type CRC32Result added in v1.7.1

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

func NewCRC32Result added in v1.7.1

func NewCRC32Result(result uint32, err error) *CRC32Result

func (*CRC32Result) Array added in v1.7.1

func (c *CRC32Result) Array() [4]byte

func (*CRC32Result) Error added in v1.7.1

func (c *CRC32Result) Error() error

func (*CRC32Result) Lower added in v1.7.1

func (c *CRC32Result) Lower() string

func (*CRC32Result) Slice added in v1.7.1

func (c *CRC32Result) Slice() []byte

func (*CRC32Result) Upper added in v1.7.1

func (c *CRC32Result) Upper() string

func (*CRC32Result) Value added in v1.7.1

func (c *CRC32Result) Value() uint32

type HTTPRespAPIModel added in v1.7.1

type HTTPRespAPIModel struct {
	Code HTTPRespCode `json:"code"`
	Msg  string       `json:"msg"`
}

func NewHTTPResp added in v1.7.1

func NewHTTPResp(code HTTPRespCode, msg string) *HTTPRespAPIModel

NewHTTPResp Util Reserved code range (-100,100)

Example
fmt.Println(NewHTTPResp(HTTPRespCodeOKCode, HTTPRespCodeOKMsg))
Output:

{"code":0,"msg":"ok"}

func ParseHTTPResp added in v1.7.1

func ParseHTTPResp(respstr string) *HTTPRespAPIModel
Example
fmt.Println(ParseHTTPResp(`{"code":0,"msg":"ok"}`).String())
Output:

{"code":0,"msg":"ok"}

func (*HTTPRespAPIModel) String added in v1.7.1

func (r *HTTPRespAPIModel) String() string

type HTTPRespCode added in v1.7.1

type HTTPRespCode int

HTTPRespCode Util Reserved code range (-100,100)

type JSON added in v1.7.1

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

func NewJSON added in v1.7.1

func NewJSON(v any, isArray bool) *JSON
Example
type TestStruct struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}
test1 := TestStruct{
	Name: "Akvicor",
	Age:  17,
}
res := NewJSON(&test1, false)
if res.Error() == nil {
	fmt.Println(res.String())
	fmt.Println(res.Bytes())
	fmt.Println(res.Map())
	fmt.Println(res.Map(0))
	fmt.Println(res.Map(10))
	fmt.Println(res.MapArray())
	fmt.Println(NewJSON(res.Map(), false))
}
test2 := [...]TestStruct{{Name: "Akvicor", Age: 17}, {Name: "MIU", Age: 17}}
res = NewJSON(&test2, true)
if res.Error() == nil {
	fmt.Println(res.Map(1))
	fmt.Println(res.MapArray())
}
Output:

{"name":"Akvicor","age":17}
[123 34 110 97 109 101 34 58 34 65 107 118 105 99 111 114 34 44 34 97 103 101 34 58 49 55 125]
map[age:17 name:Akvicor]
map[age:17 name:Akvicor]
map[age:17 name:Akvicor]
[map[age:17 name:Akvicor]]
{"age":17,"name":"Akvicor"}
map[age:17 name:MIU]
[map[age:17 name:Akvicor] map[age:17 name:MIU]]

func NewJSONResult added in v1.7.1

func NewJSONResult(data []byte) *JSON
Example
fmt.Println(NewJSONResult([]byte(`{"name":"Akvicor","age":17}`)).Map())
fmt.Println(NewJSONResult([]byte(`[{"name":"Akvicor","age":17}]`)).Map())
Output:

map[age:17 name:Akvicor]
map[age:17 name:Akvicor]

func (*JSON) Bytes added in v1.7.1

func (j *JSON) Bytes() []byte

func (*JSON) Error added in v1.7.1

func (j *JSON) Error() error

func (*JSON) Map added in v1.7.1

func (j *JSON) Map(idx ...int) map[string]any

func (*JSON) MapArray added in v1.7.1

func (j *JSON) MapArray() []map[string]any

func (*JSON) String added in v1.7.1

func (j *JSON) String() string

type MD5 added in v1.7.1

type MD5 struct{}

func NewMD5 added in v1.7.1

func NewMD5() *MD5
Example
m5 := NewMD5()
res := m5.FromString("Akvicor")
if res.Error() == nil {
	fmt.Println(res.Lower())
	fmt.Println(res.Upper())
	fmt.Println(res.Slice())
}
Output:

f812705c26adc52561415189e5f78edb
F812705C26ADC52561415189E5F78EDB
[248 18 112 92 38 173 197 37 97 65 81 137 229 247 142 219]

func (*MD5) FromBytes added in v1.7.1

func (m *MD5) FromBytes(b []byte) *MD5Result

func (*MD5) FromFile added in v1.7.1

func (m *MD5) FromFile(filepath string) *MD5Result

func (*MD5) FromFileChunk added in v1.7.1

func (m *MD5) FromFileChunk(filepath string, chunksize int) *MD5Result

func (*MD5) FromReader added in v1.7.1

func (m *MD5) FromReader(r io.Reader) *MD5Result

func (*MD5) FromReaderChunk added in v1.7.1

func (m *MD5) FromReaderChunk(r io.Reader, chunksize int) *MD5Result

func (*MD5) FromString added in v1.7.1

func (m *MD5) FromString(str string) *MD5Result

type MD5Pip added in v1.7.1

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

func NewMD5Pip added in v1.7.1

func NewMD5Pip() *MD5Pip
Example
mp := NewMD5Pip()
io.WriteString(mp, "Akvicor")
res := mp.Result()
if res.Error() == nil {
	fmt.Println(res.Array())
	fmt.Println(res.Slice())
}
Output:

[248 18 112 92 38 173 197 37 97 65 81 137 229 247 142 219]
[248 18 112 92 38 173 197 37 97 65 81 137 229 247 142 219]

func (*MD5Pip) Result added in v1.7.1

func (m *MD5Pip) Result() *MD5Result

func (*MD5Pip) Write added in v1.7.1

func (m *MD5Pip) Write(data []byte) (n int, err error)

func (*MD5Pip) WriteBytes added in v1.7.1

func (m *MD5Pip) WriteBytes(data []byte) error

func (*MD5Pip) WriteString added in v1.7.1

func (m *MD5Pip) WriteString(data string) error

type MD5Result added in v1.7.1

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

func NewMD5Result added in v1.7.1

func NewMD5Result(result []byte, err error) *MD5Result

func (*MD5Result) Array added in v1.7.1

func (m *MD5Result) Array() [MD5ResultLength]byte

func (*MD5Result) Error added in v1.7.1

func (m *MD5Result) Error() error

func (*MD5Result) Lower added in v1.7.1

func (m *MD5Result) Lower() string

func (*MD5Result) Slice added in v1.7.1

func (m *MD5Result) Slice() []byte

func (*MD5Result) Upper added in v1.7.1

func (m *MD5Result) Upper() string

type SHA1 added in v1.7.1

type SHA1 struct{}

func NewSHA1 added in v1.7.1

func NewSHA1() *SHA1
Example
s := NewSHA1()
res := s.FromString("Akvicor")
if res.Error() == nil {
	fmt.Println(res.Lower())
	fmt.Println(res.Upper())
	fmt.Println(res.Slice())
}
Output:

49296886bfedac0dd0399030bc0dbe12e5eed489
49296886BFEDAC0DD0399030BC0DBE12E5EED489
[73 41 104 134 191 237 172 13 208 57 144 48 188 13 190 18 229 238 212 137]

func (*SHA1) FromBytes added in v1.7.1

func (s *SHA1) FromBytes(b []byte) *SHA1Result

func (*SHA1) FromFile added in v1.7.1

func (s *SHA1) FromFile(filepath string) *SHA1Result

func (*SHA1) FromFileChunk added in v1.7.1

func (s *SHA1) FromFileChunk(filepath string, chunksize int) *SHA1Result

func (*SHA1) FromReader added in v1.7.1

func (s *SHA1) FromReader(r io.Reader) *SHA1Result

func (*SHA1) FromReaderChunk added in v1.7.1

func (s *SHA1) FromReaderChunk(r io.Reader, chunksize int) *SHA1Result

func (*SHA1) FromString added in v1.7.1

func (s *SHA1) FromString(str string) *SHA1Result

type SHA1Pip added in v1.7.1

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

func NewSHA1Pip added in v1.7.1

func NewSHA1Pip() *SHA1Pip
Example
sp := NewSHA1Pip()
io.WriteString(sp, "Akvicor")
res := sp.Result()
if res.Error() == nil {
	fmt.Println(res.Array())
	fmt.Println(res.Slice())
}
Output:

[73 41 104 134 191 237 172 13 208 57 144 48 188 13 190 18 229 238 212 137]
[73 41 104 134 191 237 172 13 208 57 144 48 188 13 190 18 229 238 212 137]

func (*SHA1Pip) Result added in v1.7.1

func (s *SHA1Pip) Result() *SHA1Result

func (*SHA1Pip) Write added in v1.7.1

func (s *SHA1Pip) Write(data []byte) (n int, err error)

func (*SHA1Pip) WriteBytes added in v1.7.1

func (s *SHA1Pip) WriteBytes(data []byte) error

func (*SHA1Pip) WriteString added in v1.7.1

func (s *SHA1Pip) WriteString(data string) error

type SHA1Result added in v1.7.1

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

func NewSHA1Result added in v1.7.1

func NewSHA1Result(result []byte, err error) *SHA1Result

func (*SHA1Result) Array added in v1.7.1

func (s *SHA1Result) Array() [SHA1ResultLength]byte

func (*SHA1Result) Error added in v1.7.1

func (s *SHA1Result) Error() error

func (*SHA1Result) Lower added in v1.7.1

func (s *SHA1Result) Lower() string

func (*SHA1Result) Slice added in v1.7.1

func (s *SHA1Result) Slice() []byte

func (*SHA1Result) Upper added in v1.7.1

func (s *SHA1Result) Upper() string

type SHA256 added in v1.7.1

type SHA256 struct{}

func NewSHA256 added in v1.7.1

func NewSHA256() *SHA256
Example
s := NewSHA256()
res := s.FromString("Akvicor")
if res.Error() == nil {
	fmt.Println(res.Lower())
	fmt.Println(res.Upper())
	fmt.Println(res.Slice())
}
Output:

36acc0924a190c77386cd819a3bff60251345e8654399f68e8b740a1afa62ff5
36ACC0924A190C77386CD819A3BFF60251345E8654399F68E8B740A1AFA62FF5
[54 172 192 146 74 25 12 119 56 108 216 25 163 191 246 2 81 52 94 134 84 57 159 104 232 183 64 161 175 166 47 245]

func (*SHA256) FromBytes added in v1.7.1

func (s *SHA256) FromBytes(b []byte) *SHA256Result

func (*SHA256) FromFile added in v1.7.1

func (s *SHA256) FromFile(filepath string) *SHA256Result

func (*SHA256) FromFileChunk added in v1.7.1

func (s *SHA256) FromFileChunk(filepath string, chunksize int) *SHA256Result

func (*SHA256) FromReader added in v1.7.1

func (s *SHA256) FromReader(r io.Reader) *SHA256Result

func (*SHA256) FromReaderChunk added in v1.7.1

func (s *SHA256) FromReaderChunk(r io.Reader, chunksize int) *SHA256Result

func (*SHA256) FromString added in v1.7.1

func (s *SHA256) FromString(str string) *SHA256Result

type SHA256Pip added in v1.7.1

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

func NewSHA256Pip added in v1.7.1

func NewSHA256Pip() *SHA256Pip
Example
sp := NewSHA256Pip()
io.WriteString(sp, "Akvicor")
res := sp.Result()
if res.Error() == nil {
	fmt.Println(res.Array())
	fmt.Println(res.Slice())
}
Output:

[54 172 192 146 74 25 12 119 56 108 216 25 163 191 246 2 81 52 94 134 84 57 159 104 232 183 64 161 175 166 47 245]
[54 172 192 146 74 25 12 119 56 108 216 25 163 191 246 2 81 52 94 134 84 57 159 104 232 183 64 161 175 166 47 245]

func (*SHA256Pip) Result added in v1.7.1

func (s *SHA256Pip) Result() *SHA256Result

func (*SHA256Pip) Write added in v1.7.1

func (s *SHA256Pip) Write(data []byte) (n int, err error)

func (*SHA256Pip) WriteBytes added in v1.7.1

func (s *SHA256Pip) WriteBytes(data []byte) error

func (*SHA256Pip) WriteString added in v1.7.1

func (s *SHA256Pip) WriteString(data string) error

type SHA256Result added in v1.7.1

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

func NewSHA256Result added in v1.7.1

func NewSHA256Result(result []byte, err error) *SHA256Result

func (*SHA256Result) Array added in v1.7.1

func (s *SHA256Result) Array() [SHA256ResultLength]byte

func (*SHA256Result) Error added in v1.7.1

func (s *SHA256Result) Error() error

func (*SHA256Result) Lower added in v1.7.1

func (s *SHA256Result) Lower() string

func (*SHA256Result) Slice added in v1.7.1

func (s *SHA256Result) Slice() []byte

func (*SHA256Result) Upper added in v1.7.1

func (s *SHA256Result) Upper() string

type SHA512 added in v1.7.1

type SHA512 struct{}

func NewSHA512 added in v1.7.1

func NewSHA512() *SHA512
Example
s := NewSHA512()
res := s.FromString("Akvicor")
if res.Error() == nil {
	fmt.Println(res.Lower())
	fmt.Println(res.Upper())
	fmt.Println(res.Slice())
}
Output:

6fdfb481af6573ac49d4031d22f0d73f0f24ea8cb113982593ec65ab085b0635b46d7683aa3f4ef36c2cb25a7bb8ba8cbae2fa3e9810d35b1c70a93f37586361
6FDFB481AF6573AC49D4031D22F0D73F0F24EA8CB113982593EC65AB085B0635B46D7683AA3F4EF36C2CB25A7BB8BA8CBAE2FA3E9810D35B1C70A93F37586361
[111 223 180 129 175 101 115 172 73 212 3 29 34 240 215 63 15 36 234 140 177 19 152 37 147 236 101 171 8 91 6 53 180 109 118 131 170 63 78 243 108 44 178 90 123 184 186 140 186 226 250 62 152 16 211 91 28 112 169 63 55 88 99 97]

func (*SHA512) FromBytes added in v1.7.1

func (s *SHA512) FromBytes(b []byte) *SHA512Result

func (*SHA512) FromFile added in v1.7.1

func (s *SHA512) FromFile(filepath string) *SHA512Result

func (*SHA512) FromFileChunk added in v1.7.1

func (s *SHA512) FromFileChunk(filepath string, chunksize int) *SHA512Result

func (*SHA512) FromReader added in v1.7.1

func (s *SHA512) FromReader(r io.Reader) *SHA512Result

func (*SHA512) FromReaderChunk added in v1.7.1

func (s *SHA512) FromReaderChunk(r io.Reader, chunksize int) *SHA512Result

func (*SHA512) FromString added in v1.7.1

func (s *SHA512) FromString(str string) *SHA512Result

type SHA512Pip added in v1.7.1

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

func NewSHA512Pip added in v1.7.1

func NewSHA512Pip() *SHA512Pip
Example
sp := NewSHA512Pip()
io.WriteString(sp, "Akvicor")
res := sp.Result()
if res.Error() == nil {
	fmt.Println(res.Array())
	fmt.Println(res.Slice())
}
Output:

[111 223 180 129 175 101 115 172 73 212 3 29 34 240 215 63 15 36 234 140 177 19 152 37 147 236 101 171 8 91 6 53 180 109 118 131 170 63 78 243 108 44 178 90 123 184 186 140 186 226 250 62 152 16 211 91 28 112 169 63 55 88 99 97]
[111 223 180 129 175 101 115 172 73 212 3 29 34 240 215 63 15 36 234 140 177 19 152 37 147 236 101 171 8 91 6 53 180 109 118 131 170 63 78 243 108 44 178 90 123 184 186 140 186 226 250 62 152 16 211 91 28 112 169 63 55 88 99 97]

func (*SHA512Pip) Result added in v1.7.1

func (s *SHA512Pip) Result() *SHA512Result

func (*SHA512Pip) Write added in v1.7.1

func (s *SHA512Pip) Write(data []byte) (n int, err error)

func (*SHA512Pip) WriteBytes added in v1.7.1

func (s *SHA512Pip) WriteBytes(data []byte) error

func (*SHA512Pip) WriteString added in v1.7.1

func (s *SHA512Pip) WriteString(data string) error

type SHA512Result added in v1.7.1

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

func NewSHA512Result added in v1.7.1

func NewSHA512Result(result []byte, err error) *SHA512Result

func (*SHA512Result) Array added in v1.7.1

func (s *SHA512Result) Array() [SHA512ResultLength]byte

func (*SHA512Result) Error added in v1.7.1

func (s *SHA512Result) Error() error

func (*SHA512Result) Lower added in v1.7.1

func (s *SHA512Result) Lower() string

func (*SHA512Result) Slice added in v1.7.1

func (s *SHA512Result) Slice() []byte

func (*SHA512Result) Upper added in v1.7.1

func (s *SHA512Result) Upper() string

Jump to

Keyboard shortcuts

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