mao-gutils README
基于日常学习及项目需要所积累的go语言常用工具库
byte
-
func Int64ToBytes(i int64) []byte
-
func BytesToInt64(buf []byte) int64
dingtalk
-
func DingTalkAlarm(serverName, message string) bool
func DingTalkAlarmUrl(url, serverName, message string) bool
encryption
-
func AESEncrypt(msg, key []byte) []byte func AESEncryptE(msg, key []byte) ([]byte, error) func AESEncryptIv(msg, key, iv []byte) ([]byte, error)
-
func AESDecrypt(msg, key []byte) []byte func AESDecryptE(msg, key []byte) ([]byte, error) func AESDecryptIv(msg, key, iv []byte) ([]byte, error)
-
func RSAEncrypt(public []byte, msg []byte) []byte func RSAEncryptE(public []byte, msg []byte) ([]byte, error) func RSAEncryptPKCS1(public []byte, msg []byte) []byte func RSAEncryptPKCS1E(public []byte, msg []byte) ([]byte, error) func RSAEncryptPem(public []byte, msg []byte) []byte func RSAEncryptPemE(public []byte, msg []byte) ([]byte, error) func RSAEncryptPKCS1Pem(public []byte, msg []byte) []byte func RSAEncryptPKCS1PemE(public []byte, msg []byte) ([]byte, error) func RSAEncryptString(public, msg []byte) []byte func RSAEncryptStringE(public, msg []byte) ([]byte, error) func RSAEncryptPKCS1String(public, msg []byte) []byte func RSAEncryptPKCS1StringE(public, msg []byte) ([]byte, error)
-
// RSADecrypt 使用私钥进行解密 func RSADecrypt(private []byte, cipherText []byte) []byte // RSADecryptPKCS1 使用私钥进行解密 func RSADecryptPKCS1(private []byte, cipherText []byte) []byte // RSADecryptE 使用私钥进行解密 func RSADecryptE(private []byte, cipherText []byte) ([]byte, error) // RSADecryptPKCS1E 使用私钥进行解密 func RSADecryptPKCS1E(private []byte, cipherText []byte) ([]byte, error) // RSADecryptPem 使用私钥(pem格式)进行解密 func RSADecryptPem(private []byte, cipherText []byte) []byte // RSADecryptPKCS1Pem 使用私钥(pem格式)进行解密 func RSADecryptPKCS1Pem(private []byte, cipherText []byte) []byte // RSADecryptString 使用私钥(String格式)进行解密 func RSADecryptString(private, cipherText []byte) []byte // RSADecryptPKCS1String 使用私钥(String格式)进行解密 func RSADecryptPKCS1String(private, cipherText []byte) []byte // RSADecryptPemE 使用私钥(pem格式)进行解密 func RSADecryptPemE(private []byte, cipherText []byte) ([]byte, error) // RSADecryptStringE 使用私钥(String格式)进行解密
hash
-
func Hash64Byte(data []byte) uint64 func Hash64(data string) uint64 func Hash32Byte(data []byte) uint32 func Hash32(data string) uint32
html
-
func TrimHtml(src string) string
http
-
func HttpDO(method string, url string, body io.Reader, header map[string][]string) (httpStatus int, resp []byte, err error) func HttpDOTimeOut(method string, url string, body io.Reader, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error)
-
func HttpPost(url string, body io.Reader, header map[string][]string) (httpStatus int, resp []byte, err error)
-
func HttpGet(url string, body io.Reader, header map[string][]string) (httpStatus int, resp []byte, err error)
-
func HttpPostJson(url string, body []byte, header map[string][]string) (httpStatus int, resp []byte, err error) func HttpPostTimeOut(url string, body io.Reader, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error) func HttpPostJsonTimeOut(url string, body []byte, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error)
-
func HttpGetJson(url string, body []byte, header map[string][]string) (httpStatus int, resp []byte, err error) func HttpGetTimeOut(url string, body io.Reader, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error) func HttpGetJsonTimeOut(url string, body []byte, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error)
-
func HttpPostForm(postUrl string, body map[string][]string, header map[string][]string) (httpStatus int, resp []byte, err error) func HttpPostFormTimeOut(postUrl string, body map[string][]string, header map[string][]string, millisecond int) (httpStatus int, resp []byte, err error)
interface
-
func JudgeType(v interface{}) string
-
func JudgeTypeByReflect(v interface{}) string
json
-
func JSON2Object(data []byte, obj interface{}) func JSON2ObjectE(data []byte, obj interface{}) func JSON2ObjectUseNumberE(data []byte, obj interface{}) (err error) func Object2JSON(obj interface{}) string func Object2JSONByte(obj interface{}) []byte func Object2JSONByteE(obj interface{}) ([]byte, error) func Object2JSONE(obj interface{}) (string, error) func JSON2Map(json []byte) map[string]interface{} func JSON2MapUseNumber(json []byte) map[string]interface{} // Valid 验证JSON字符串是否合法。此方法只验证标准格式的,开头和结尾为{} // jsoniter.Valid方法“abc”也可以验证通过 func Valid(json []byte) bool func GzipEncode(body []byte) (result []byte) func GzipDecode(body []byte) (result []byte) func HuffmanEncode(body []byte) (result []byte) func HuffmanDecode(body []byte) (result []byte)
map
-
// 方法1(效率很高): func getMapKeys1(m map[int]int) []int
// 方法2(效率很高): func getMapKeys2(m map[int]int) []int
// 方法3(效率较低): func getMapKeys3(m map[int]int) []int
// 方法4(效率极低): func getMapKeys4(m map[int]int) int
-
func FilterSliceByMap(filter map[string]struct{}, pram []string, isExist bool) (array []string)
-
func FilterMapByMap(filter map[string]struct{}, pram map[string]string, isExist bool) (array map[string]string)
-
func MergeMap(m1, m2 map[string]interface{}) map[string]interface{}
-
func UniqueArray(m []string) []string
-
func MapSplitByComma(fields map[string]string) []map[string]string
net
-
func IsIP4(ip string) bool
-
func IsIP6(ip string) bool
-
// 0: invalid ip // 4: IPv4 // 6: IPv6 func ParseIP(s string) int
-
func IsPrivateIP(ipStr string) bool
-
func InternalIp() string
os
-
func ExpandByMap(template string, fields map[string]string) string
-
func ExpandByFun(str string, f func(string) string) string
-
/** * 输入示例: * "command_fields": { * "user_id": "1,2,3", * "prop": "huChao" * } * * template: "chartid=${user_id}&prop=${prop}" * * 输出示例: * [ * "chartid=1&prop=hudaoju", * "chartid=2&prop=hudaoju", * "chartid=3&prop=hudaoju" * ] * */ // isMultiple: 是否根据 "," 分割 func GetComposedTemplateListExpandByMap(template string, isMultiple bool, fields map[string]string) []string
slice
-
func removeRepByMap(slc []*SeriesRes) []*SeriesRes
-
func removeStructRepByMap(slc []*SeriesRes) []*SeriesRes
-
func SliceToLinkList(nums []int, head *ListNode) *ListNode
strings
-
func TrimSpace(str string) string
-
func VersionOrdinal(version string) string
-
func VersionGreater(versionA, versionB string) int
-
func StringJoin(strs ...string) string
-
func IsExactExist(array []string, row string) bool
-
func Str2Int32Array(strArray []string) ([]int32, error)
struct
-
func StructCopy(src, dst interface{}) (err error)
time
-
func NowTime() string
-
func NowUnix() int64
-
func UnixToFormatTime(timeStamp int64) string
-
func GetBetweenMonths(startTime, endTime time.Time) ([]string, error) // 月 func GetBetweenDates(startTime, endTime time.Time) ([]string, error) // 日
-
func FormatTimeIfNotZero(time time.Time, layout string) string
time - 时区
-
func DateCST(layout string) string
-
func DatePST(layout string) string
-
func DateJST(layout string) string
-
func DateKST(layout string) string
-
func DateUTC(layout string) string
uuid
-
func UUID() string
-
func UUID32() string
-
func MD5(body []byte) string
-
func PramSign(pram []string) string
version
-
//VersionOrdinal 返回可比较的字符串,当传入非法ASCII码时返回空字符串 //用于版本比较 func VersionOrdinal(version string) string
-
//VersionGreater 比较两个版本号。版本号只有数字和点组成 // 如:versionA == versionB 返回 0 // 如:versionA > versionB 返回 1 // 如:versionA < versionB 返回 -1 func VersionGreater(versionA, versionB string) int
-
//VersionCheck 验证版本,该方法只支持app市场版本的格式:主版本.此版本.修订版本.热更版本。(且可以使用0开头) //理论最大版本号:999.999.999.999 func VersionCheck(v string) bool
-
//VersionApp 通过传入的版本号获取app的市场版本。 //app的市场版本格式为:x.x.x func VersionApp(v string) string
-
//VersionAppByCount 通过传入的版本号和count确定返回几位的版本号 //v:版本号 //count:需要返回的版本号位数 func VersionAppByCount(v string, count int) string
持续更新,欢迎pr👏
- 微信公众号:Gopher毛
- 交流QQ群3: 805360166(活跃)
- 交流QQ群2: 579480724
- 交流QQ群1:1007576722