Documentation ¶
Index ¶
- Constants
- Variables
- func AesCBCDecrypt(cipherStr string, key string, iv string) (string, error)
- func AesCBCEncrypt(text string, key string, iv string) (string, error)
- func Base64Decode(str string) string
- func Base64Encode(str string) string
- func Base64UrlDecode(str string) string
- func Base64UrlEncode(str string) string
- func Blank(str string) bool
- func BlankAll(strs ...string) bool
- func BlankAny(strs ...string) bool
- func Bytes(s string) []byte
- func CamelToSnake(str string) string
- func Command(bin string, argv []string, baseDir string) ([]byte, error)
- func Contains(str, substr string) bool
- func ContainsAny(str string, substr ...string) bool
- func ContainsCase(str, substr string) bool
- func Date(layouts ...any) string
- func Empty(value any) bool
- func EmptyAll(values ...any) bool
- func EmptyAny(values ...any) bool
- func FileExists(path string) bool
- func HasPrefixCase(str, prefix string) bool
- func HasSuffixCase(str, prefix string) bool
- func HttpDelete(urlStr string, args ...any) ([]byte, error)
- func HttpDeleteDo(urlStr string, r *HttpReq, timeout int) ([]byte, error)
- func HttpDo(req *http.Request, r *HttpReq, timeout int) ([]byte, error)
- func HttpGet(urlStr string, args ...any) ([]byte, error)
- func HttpGetDo(urlStr string, r *HttpReq, timeout int) ([]byte, error)
- func HttpPost(urlStr string, args ...any) ([]byte, error)
- func HttpPostDo(urlStr string, body io.Reader, r *HttpReq, timeout int) ([]byte, error)
- func HttpPostForm(urlStr string, args ...any) ([]byte, error)
- func HttpPostFormDo(urlStr string, posts map[string]string, r *HttpReq, timeout int) ([]byte, error)
- func HttpPostJson(urlStr string, args ...any) ([]byte, error)
- func HttpPostJsonDo(urlStr string, json string, r *HttpReq, timeout int) ([]byte, error)
- func HttpPut(urlStr string, args ...any) ([]byte, error)
- func HttpPutDo(urlStr string, body io.Reader, r *HttpReq, timeout int) ([]byte, error)
- func HttpPutForm(urlStr string, args ...any) ([]byte, error)
- func HttpPutFormDo(urlStr string, posts map[string]string, r *HttpReq, timeout int) ([]byte, error)
- func HttpPutJson(urlStr string, args ...any) ([]byte, error)
- func HttpPutJsonDo(urlStr string, json string, r *HttpReq, timeout int) ([]byte, error)
- func If[T any](condition bool, trueVal, falseVal T) T
- func IntsToStrings(slice []int) []string
- func Ip2Long(ipStr string) uint32
- func IsASCII(s string) bool
- func IsASCIILetter(str string) bool
- func IsDir(path string) bool
- func IsEmail(str string) bool
- func IsExist(path string) bool
- func IsIp(ipStr string) bool
- func IsIpV4(ipStr string) bool
- func IsIpV6(ipStr string) bool
- func IsLetter(str string) bool
- func IsNumber(str string) bool
- func IsUtf8(p []byte) bool
- func Long2Ip(long uint32) string
- func LongestCommonSubString(x, y string) int
- func MapKeys[K comparable, V any](m map[K]V) []K
- func MapMerge[K comparable, V any](maps ...map[K]V) map[K]V
- func MapValues[K comparable, V any](m map[K]V) []V
- func Matches(str, pattern string) bool
- func Max(a, b int) int
- func MaxInt64(a, b int64) int64
- func MaxN[T GenNumber](args ...T) T
- func Md5(str string) string
- func Md5Bit16(str string) string
- func Memory(format string) map[string]int64
- func MemoryBytes() map[string]int64
- func Min(a, b int) int
- func MinInt64(a, b int64) int64
- func MinN[T GenNumber](args ...T) T
- func Mkdir(dir string, perm os.FileMode) error
- func NormaliseLine(str string) string
- func NormaliseSpace(str string) string
- func PadBoth(str string, padStr string, padLen int) string
- func PadLeft(str string, padStr string, padLen int) string
- func PadRight(str string, padStr string, padLen int) string
- func Random() int
- func RandomInt(min, max int) int
- func RandomInt64(min, max int64) int64
- func RandomLetter(length int) string
- func RandomNumber(length int) string
- func RandomPool(pool string, length int) string
- func RandomString(length int) string
- func Remove(str, remove string) string
- func RemoveAny(str string, removes ...string) string
- func RemoveLines(str string) string
- func RemovePrefix(str, prefix string) string
- func RemoveSign(str string) string
- func RemoveSuffix(str string, suffix string) string
- func Reverse(str string) string
- func Sha1(str string) string
- func Sha256(str string) string
- func Sha384(str string) string
- func Sha512(str string) string
- func Similarity(a, b string) float64
- func SimilarityText(a, b string) float64
- func SliceColumn[T, V any](slice []T, key any) []V
- func SliceConcat[T any](slice []T, values ...[]T) []T
- func SliceContains[T comparable](slice []T, v T) bool
- func SliceEqual[T comparable](slice1, slice2 []T) bool
- func SliceEvery[T any](slice []T, predicate func(index int, item T) bool) bool
- func SliceFilter[T any](slice []T, predicate func(index int, item T) bool) []T
- func SliceForEach[T any](slice []T, iteratee func(index int, item T))
- func SliceIndex[T comparable](slice []T, v T) int
- func SliceIntersection[T comparable](slices ...[]T) []T
- func SliceLastIndex[T comparable](slice []T, v T) int
- func SliceMap[T any, U any](slice []T, iteratee func(index int, item T) U) []U
- func SliceNone[T any](slice []T, predicate func(index int, item T) bool) bool
- func SliceReduce[T any](slice []T, iteratee func(index int, result, item T) T, initial T) T
- func SliceRemove[T comparable](slice []T, v T) []T
- func SliceRemoveBlank(slice []string) []string
- func SliceReplace[T comparable](slice []T, old T, new T, n int) []T
- func SliceReplaceAll[T comparable](slice []T, old T, new T) []T
- func SliceSome[T any](slice []T, predicate func(index int, item T) bool) bool
- func SliceSortBy(slice any, field string, sortType ...string) error
- func SliceSplit[T comparable](slice []T, size int) [][]T
- func SliceTrim(slice []string) []string
- func SliceUnion[T comparable](slices ...[]T) []T
- func SliceUnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
- func SliceUnique[T comparable](slice []T) []T
- func SnakeToCamel(str string, bigCamel bool) string
- func SplitTrim(str, sep string) []string
- func SplitTrimToInts(str, sep string) []int
- func StrAfter(s, char string) string
- func StrAfterLast(s, char string) string
- func StrBefore(s, char string) string
- func StrBeforeLast(s, char string) string
- func StrToTime(args ...any) int64
- func String(b []byte) string
- func StringsToInts(slice []string) []int
- func StructCopy(src, dst any)
- func SubString(str string, pos, length int) string
- func Template(tpl string, data any) (string, error)
- func Timestamp(millis ...any) int64
- func ToBool(str string) bool
- func ToDuration(value any) time.Duration
- func ToDurationMs(value any) time.Duration
- func ToFloat32(value any) float32
- func ToFloat64(value any) float64
- func ToInt(value any) int
- func ToInt64(value any) int64
- func ToJson(object any) string
- func ToJsonIndent(object any) string
- func ToLong(value any) int64
- func ToString(value any) string
- func ToUint(value any) uint
- func ToUint8(value any) uint8
- func ToUtf8(origin []byte, encode string) ([]byte, error)
- func Unwrap(str string, wrapStr string) string
- func UrlParse(rawURL string) (*url.URL, error)
- func UserAgentRandom() string
- func UserAgentRandomMobile() string
- func Utf8To(utf8 []byte, encode string) ([]byte, error)
- func Wrap(str string, wrapStr string) string
- func WriteFile(name string, data []byte, flag int, perm os.FileMode, sync bool) error
- func WriteFileAppend(name string, data []byte, perm os.FileMode, sync bool) error
- type GenFloat
- type GenInteger
- type GenNumber
- type HttpReq
- type HttpResp
- func HttpDeleteResp(urlStr string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpDoResp(req *http.Request, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpGetResp(urlStr string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPostFormResp(urlStr string, posts map[string]string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPostJsonResp(urlStr string, json string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPostResp(urlStr string, body io.Reader, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPutFormResp(urlStr string, posts map[string]string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPutJsonResp(urlStr string, json string, r *HttpReq, timeout int) (*HttpResp, error)
- func HttpPutResp(urlStr string, body io.Reader, r *HttpReq, timeout int) (*HttpResp, error)
Constants ¶
const ( SPACE = " " DOT = "." SLASH = "/" UNDERSCORE = "_" COLON = ":" DASH = "-" LF = "\n" CRLF = "\r\n" CR = "\r" TAB = "\t" )
const ( DatePattern = "2006-01-02" DatePatternSlash = "2006/01/02" DatePatternUnderscore = "2006_01_02" DatePatternZh = "2006年01月02日" DatetimePattern = "2006-01-02 15:04:05" DatetimeMilliPattern = "2006-01-02 15:04:05.999" DatetimePatternSlash = "2006/01/02 15:04:05" DatetimeMilliPatternSlash = "2006/01/02 15:04:05.999" DatetimePatternZh = "2006年01月02日 15时04分05秒" DatetimePatternUtc = "2006-01-02'T'15:04:05'Z'" )
const ( StringNumber = "0123456789" StringUpperLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" StringLowerLetter = "abcdefghijklmnopqrstuvwxyz" StringLetter = StringUpperLetter + StringLowerLetter StringLetterAndNumber = StringLetter + StringNumber )
const ( MimePlain = "text/plain" MimeHtml = "text/html" MimeJson = "application/json" MimePostForm = "application/x-www-form-urlencoded" MimeMultipartPostForm = "multipart/form-data" MimeProtobuf = "application/x-protobuf" MimeYaml = "application/x-yaml" )
const ( SizeB = "B" SizeKB = "KB" SizeMB = "MB" SizeGB = "GB" SizeTB = "TB" SizePB = "PB" SizeEB = "EB" )
const ( BytesPerKB BytesPerMB BytesPerGB BytesPerTB BytesPerPB BytesPerEB )
const ( HttpDefaultTimeOut = 10000 HttpDefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" HttpDefaultAcceptEncoding = "gzip, deflate" )
const ( RegexLetter string = "^[a-zA-Z]+$" RegexLetterNumber string = "^[a-zA-Z0-9]+$" RegexNumber string = "^[0-9]+$" RegexChinese string = "^[\u4e00-\u9fa5]+$" RegexEmail string = "" /* 1277-byte string literal not displayed */ RegexDateTime string = "" /* 235-byte string literal not displayed */ )
Variables ¶
var ( RegexEmailPattern = regexp.MustCompile(RegexEmail) RegexDateTimePattern = regexp.MustCompile(RegexDateTime) )
var HttpDefaultTransport = &http.Transport{ DialContext: (&net.Dialer{Timeout: time.Second}).DialContext, ForceAttemptHTTP2: true, MaxIdleConns: 150, MaxIdleConnsPerHost: 3, IdleConnTimeout: 60 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }
HttpDefaultTransport 默认全局使用的 http.Transport
Functions ¶
func AesCBCDecrypt ¶ added in v0.85.0
AesCBCDecrypt Aes CBC 对称加密
func AesCBCEncrypt ¶ added in v0.85.0
AesCBCEncrypt Aes CBC 对称加密, key 的长度决定 AES-128, AES-192, or AES-256
func Base64UrlDecode ¶
Base64UrlDecode 返回 Url Safe Base64 值对应的字符串
func Base64UrlEncode ¶
Base64UrlEncode 返回字符串 Url Safe Base64 值
func ContainsAny ¶
ContainsAny 判断字符串是否包含任意一个指定的多个子串
func FileExists ¶ added in v0.89.0
FileExists 检测目录或者文件是否存在,返回 bool
func HasPrefixCase ¶
HasPrefixCase 判断字符串是否以指定前缀开头, 忽略大小写
func HasSuffixCase ¶
HasSuffixCase 判断字符串是否以指定后缀结尾, 忽略大小写
func HttpDelete ¶
HttpDelete 参数为请求地址 (HttpReq, 超时时间) HttpDelete(url)、HttpDelete(url, HttpReq)、HttpDelete(url, timeout)、HttpDelete(url, HttpReq, timeout) 返回 body, 错误信息
func HttpDeleteDo ¶
HttpDeleteDo Http Delete 请求, 参数为请求地址, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func HttpGet ¶
HttpGet 参数为请求地址 (HttpReq, 超时时间) HttpGet(url)、HttpGet(url, HttpReq)、HttpGet(url, timeout)、HttpGet(url, HttpReq, timeout) 返回 body, 错误信息
func HttpPost ¶
HttpPost 参数为请求地址 (body io.Reader, HttpReq, 超时时间) HttpPost(url)、HttpPost(url, timeout)、HttpPost(url, body)、HttpPost(url, body, timeout)、HttpPost(url, body, HttpReq)、HttpPostForm(url, body, HttpReq, timeout) 返回 body, 错误信息
func HttpPostDo ¶
HttpPostDo Http Post, 参数为请求地址, body io.Reader, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func HttpPostForm ¶
HttpPostForm 参数为请求地址 (Form 数据 map[string]string, HttpReq, 超时时间) HttpPostForm(url)、HttpPostForm(url, timeout)、HttpPostForm(url, posts)、HttpPostForm(url, posts, timeout)、HttpPostForm(url, posts, HttpReq)、HttpPostForm(url, posts, HttpReq, timeout) 返回 body, 错误信息
func HttpPostFormDo ¶
func HttpPostFormDo(urlStr string, posts map[string]string, r *HttpReq, timeout int) ([]byte, error)
HttpPostFormDo Http Post Form, 参数为请求地址, Form 数据 map[string]string, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func HttpPostJson ¶
HttpPostJson 参数为请求地址 (Json 数据 string, HttpReq, 超时时间) HttpPostJson(url)、HttpPostJson(url, timeout)、HttpPostJson(url, json)、HttpPost(url, json, timeout)、HttpPost(url, json, HttpReq)、HttpPost(url, json, HttpReq, timeout) 返回 body, 错误信息
func HttpPostJsonDo ¶
HttpPostJsonDo Http Post Json 请求, 参数为请求地址, Json 数据 string, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func HttpPut ¶
HttpPut 参数为请求地址 (body io.Reader, HttpReq, 超时时间) HttpPut(url)、HttpPut(url, timeout)、HttpPut(url, body)、HttpPut(url, body, timeout)、HttpPut(url, body, HttpReq)、HttpPut(url, body, HttpReq, timeout) 返回 body, 错误信息
func HttpPutForm ¶
HttpPutForm 参数为请求地址 (Form 数据 map[string]string, HttpReq, 超时时间) HttpPutForm(url)、HttpPutForm(url, timeout)、HttpPutForm(url, posts)、HttpPutForm(url, posts, timeout)、HttpPutForm(url, posts, HttpReq)、HttpPutForm(url, posts, HttpReq, timeout) 返回 body, 错误信息
func HttpPutFormDo ¶
HttpPutFormDo Http Put Form, 参数为请求地址, Form 数据 map[string]string, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func HttpPutJson ¶
HttpPutJson 参数为请求地址 (Json 数据 string, HttpReq, 超时时间) HttpPutJson(url)、HttpPutJson(url, timeout)、HttpPutJson(url, json)、HttpPutJson(url, json, timeout)、HttpPutJson(url, json, httpReq)、HttpPutJson(url, json, httpReq, timeout) 返回 body, 错误信息
func HttpPutJsonDo ¶
HttpPutJsonDo Http Put Json 请求, 参数为请求地址, Json 数据 string, HttpReq, 超时时间(毫秒) 返回 body, 错误信息
func LongestCommonSubString ¶
LongestCommonSubString 计算两个字符串最大公共子串长度
func MapMerge ¶
func MapMerge[K comparable, V any](maps ...map[K]V) map[K]V
MapMerge 合并多个map, 如果有相同的键, 则后者会覆盖前者
func MemoryBytes ¶
MemoryBytes 返回当前主要的内存指标信息 (ReadMemStats 会 stopTheWorld, 谨慎非频繁使用)
func NormaliseSpace ¶
NormaliseSpace 规范化此字符串中的空白, 多个空格合并为一个空格, 所有空白字符例如换行符、制表符, 都转换为一个简单的空格。
func RemoveLines ¶
RemoveLines 移除换行符, 换行符包括 \n \r\n, 性能原因, 不使用 strings.NewReplacer
func RemoveSign ¶
RemoveSign 将字符串的所有数据依次写成一行, 去除无意义字符串(标点符号、符号), 性能原因, 不使用 strings.NewReplacer
func SliceConcat ¶
func SliceConcat[T any](slice []T, values ...[]T) []T
SliceConcat 合并多个切片, 非去重, 非原始切片
func SliceContains ¶
func SliceContains[T comparable](slice []T, v T) bool
SliceContains 判断整型和字符串是否在切片中
func SliceEqual ¶
func SliceEqual[T comparable](slice1, slice2 []T) bool
SliceEqual 切片是否相等: 长度相同且所有元素的顺序和值相等
func SliceEvery ¶
SliceEvery 切片中的所有元素都满足函数,则返回 true
func SliceFilter ¶
SliceFilter 筛选出切片中满足函数的所有元素
func SliceForEach ¶
SliceForEach 切片中所有元素都执行函数
func SliceIntersection ¶
func SliceIntersection[T comparable](slices ...[]T) []T
SliceIntersection 切片交集且去重(顺序不能保证)
func SliceLastIndex ¶
func SliceLastIndex[T comparable](slice []T, v T) int
SliceLastIndex 对数值和字符串切片按照指定值进行查找, 返回最后一个匹配的索引
func SliceReduce ¶
SliceReduce 处理所有切片中元素得到结果
func SliceReplace ¶
func SliceReplace[T comparable](slice []T, old T, new T, n int) []T
SliceReplace 返回切片的副本,前n个元素替换为新的
func SliceReplaceAll ¶
func SliceReplaceAll[T comparable](slice []T, old T, new T) []T
SliceReplaceAll 返回切片的副本,所有匹配到的元素都替换为新的
func SliceSortBy ¶
SliceSortBy 根据字段排序(field的大小写应该和字段保持一致)
func SliceSplit ¶
func SliceSplit[T comparable](slice []T, size int) [][]T
SliceSplit 对数值和字符串切片按照指定长度进行分割
func SliceUnionBy ¶
func SliceUnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
SliceUnionBy 顺序合并且去重, 支持自定义函数
func SliceUnique ¶
func SliceUnique[T comparable](slice []T) []T
SliceUnique 对数值和字符串切片进行去重(会改变元素的顺序)
func SplitTrimToInts ¶
SplitTrimToInts 分割字符串为 int 切片, 对分割后的值进行 Trim , 并自动忽略空值
func ToDuration ¶ added in v0.90.0
ToDuration 将数字、字符串转换为 time.Duration,默认是 ns, 如果是字符串,支持 ns,ms,us,s,m,h
func ToDurationMs ¶ added in v0.90.0
ToDurationMs 将数字、字符串转换为 time.Duration,默认是 ms, 如果是字符串,支持 ns,ms,us,s,m,h
func ToJsonIndent ¶ added in v0.90.0
ToJsonIndent 将对象转换为 Json 字符串, 带缩进
func UserAgentRandom ¶ added in v0.92.0
func UserAgentRandom() string
UserAgentRandom generates a random DESKTOP browser user-agent on every requests
func UserAgentRandomMobile ¶ added in v0.92.0
func UserAgentRandomMobile() string
UserAgentRandomMobile generates a random MOBILE browser user-agent on every requests
Types ¶
type GenInteger ¶
type GenInteger interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 }
GenInteger 整型范型集合
type HttpReq ¶
type HttpReq struct { // UserAgent 优先于请求头 Headers 中的 User-Agent 字段 UserAgent string // 请求头 Headers map[string]string // 限制最大返回大小 MaxContentLength int64 // 限制允许访问 ContentType 列表, 前缀匹配 AllowedContentTypes []string // 最大 Redirect 次数, 范围 [0,10), 否则采用默认的跳转策略 (最大限制 10 次) MaxRedirect int // 禁止跳转 DisableRedirect bool // 请求失败错误时仍然读取 Body ReadBodyWithFail bool // http.Transport Transport http.RoundTripper // http.CookieJar Jar http.CookieJar }
type HttpResp ¶
type HttpResp struct { // 是否成功 (200-299), 成功仍可能返回 err Success bool // Http 状态码 StatusCode int // 响应体 Body []byte // ContentLength (字节数) ContentLength int64 // 响应头 Headers *http.Header // 最后请求 RequestURL *url.URL }
func HttpDeleteResp ¶
HttpDeleteResp Http Delete 请求, 参数为请求地址, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpDoResp ¶
HttpDoResp Http 请求, 参数为 http.Request, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpGetResp ¶
HttpGetResp Http Get 请求, 参数为请求地址, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpPostFormResp ¶
func HttpPostFormResp(urlStr string, posts map[string]string, r *HttpReq, timeout int) (*HttpResp, error)
HttpPostFormResp Http Post Form, 参数为请求地址, Form 数据 map[string]string, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpPostJsonResp ¶
HttpPostJsonResp Http Post Json 请求, 参数为请求地址, Json 数据 string, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpPostResp ¶
HttpPostResp Http Post, 参数为请求地址, body io.Reader, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpPutFormResp ¶
func HttpPutFormResp(urlStr string, posts map[string]string, r *HttpReq, timeout int) (*HttpResp, error)
HttpPutFormResp Http Put Form, 参数为请求地址, Form 数据 map[string]string, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息
func HttpPutJsonResp ¶
HttpPutJsonResp Http Put Json 请求, 参数为请求地址, Json 数据 string, HttpReq, 超时时间(毫秒) 返回 HttpResp, 错误信息