1 Star 0 Fork 0

qingbing / go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

go 语言通用函数包

1. 下载

go get gitee.com/duqingbing/go-utils
# 指定某个 commitId 进行拉取
go get -u gitee.com/duqingbing/go-utils@commitId

2. 支持函数

  • 常量: constant
    • 启用状态
      • STATUS_DISABLE, DISABLE uint8: 0
      • STATUS_ENABLE, ENABLE uint8: 1
    • 状态开关
      • SWITCH_OFF, OFF uint8: 0
      • SWITCH_ON, ON uint8: 1
    • 星期
      • SUNDAY, SUM uint8: 0
      • MONDAY, MON uint8: 1
      • TUESDAY, TUE uint8: 2
      • WEDNESDAY, WED uint8: 3
      • THURSDAY, THU uint8: 4
      • FRIDAY, FRI uint8: 5
      • SATURDAY, SAT uint8: 6
    • 月份
      • JANUARY, JAN uint8: 1
      • FEBRUARY, FEB uint8: 2
      • MARCH, MAR uint8: 3
      • APRIL, APR uint8: 4
      • MAY uint8: 5
      • JUNE, JUN uint8: 6
      • JULY, JUL uint8: 7
      • AUGUST, AUG uint8: 8
      • SEPTEMBER, SEP uint8: 9
      • OCTOBER, OCT uint8: 10
      • NOVEMBER, NOV uint8: 11
      • DECEMBER, DEC uint8: 12
    • 性别
      • TGender uint8
      • GENDER_UNKNOWN TGender: 0
      • GENDER_FEMALE TGender: 1
      • GENDER_MALE TGender: 2
    • 内存容量
      • TGender uint64
      • MEM_SIZE_BYTE, BYTE TMemSize
      • MEM_SIZE_KB, KB TMemSize
      • MEM_SIZE_MB, MB TMemSize
      • MEM_SIZE_GB, GB TMemSize
      • MEM_SIZE_TB, TB TMemSize
      • MEM_SIZE_PB, PB TMemSize
  • 基础: basic
    • type KMap map[string]interface{}: key-value 映射
    • type EStruct = struct{}: 空 结构体类型
    • type KStructMap map[string]EStruct: key 为字符串, value 为空结构的 map
      • ToSlice(): 转换成 KSlice
    • type KSlice []string: string 类型的切片
      • ToMap(): 转换成 KStructMap
    • type XzTestingCase struct {Name string,Args KMap,Want KMap}: 通用测试示例
    • NewTesting(): 单元测试工具
  • 加解码: crypt
    • Base64Encode(v string) string: base64 编码
    • Base64Decode(v string) (str string, err error): base64 解码
    • JsonEncode(data any) (string, error): json 编码
    • JsonDecode(data string, obj any) error: json 解码
    • func Md5(v string) string: md5 编码
  • 调试: debug
    • func CallerStack(isPrint bool) string: 获取或返回调用处的调用栈
  • 文件: file
    • FileList(dir string, ignores KSlice, recursion bool) (res []string, err error): 获取目录下的文件列表
    • CopyFile(src, dist string) (copySize int, err error): 复制文件
    • CopyDir(src, dist string, ignores KSlice) error: 复制目录
  • map-structure转换: mapstructure
    • NewMapStructure(): map-structure 转换工具
  • 随机: random
    • Random
      • Int(n int) int: 获取 [0, n) 之间的整数
      • IntRange(min, max int) int: 获取 [min, max) 之间的整数
    • string
      • RandString(length uint8, mask uint8) (str string): 获取随机字符串, 可通过掩码选中字符串, mask 7 => 1,2,4, mask 3 => 1,2, mask 5 => 1, 4, 掩码最大目前为 31
      • Uniqid() string: 根据 当前时间+服务IP+当前进程ID 计算一个唯一 id
  • 队列: xzlist
    • NewXzList[T any]() XzList[T]: 队列
  • 时间: xztime
    • Time: 类型
      • 已指定 json.Unmarshal 和 json.Marshal 的处理方式
    • Now() Time: 当时时间的 xztime.Time
    • Parse(layout, value string) (time Time, err error): 根据字符及格式解析成 xztime.Time
  • 工具包: utils
    • ip 获取
      • ServerIp() (ip string): 服务端 IP
      • HasLocalIPAddr(ip string) bool: 检测 IP 地址字符串是否是内网地址
      • ClientIP(r *http.Request) string: 获取客户端 IP
      • ClientPublicIP(r *http.Request) string: 获取客户端公网 IP
      • RemoteIP(r *http.Request) string: 通过 RemoteAddr 获取 IP 地址, 快速解析方法, 不一定准确
      • IPString2Long(ip string) (uint, error): ip 字符串转为数值
      • Long2IPString(i uint) (string, error): 把数值转为 ip 字符串
    • 进制转换
      • FillWidth(str string, width int) string: 数据前位 0 补足到指定宽度的倍数
      • HexToBinary(hex string) (bin string, err error): 十六进制 转换成 二进制
      • BinaryToHex(bin string) (hex string, err error): 二进制 转换成 十六进制
      • StringToHex(str string) (bin string): 字符串 转换成 十六进制
      • HexToString(str string) (string, error): 十六进制 转换成 字符串
      • StringToBinary(str string) (string, error): 字符串 转换成 二进制
      • BinaryToString(bin string) (string, error): 二进制 转换成 字符串
    • 计时器
      • NewTimeCounter() timeCounter: 创建计时器容器
        • Begin(flag string): 开始一个计时器
        • End(flag string) float64: 结束一个计时器
    • struct
      • SetFieldValue(binding interface{}, name string, value interface{}) error: 为结构体的指定属性赋值
      • StructToStruct(binding interface{}, value interface{}) error: 将 结构体 value 的值赋值给 结构体 binding
    • 辅助函数
      • IsZero(v interface{}) bool: 判断一个变量是否零值

3. 测试用例使用示例

package goutils

import (
	"testing"
)

func sum(a, b int) int {
	return a + b
}

func TestTesting(t *testing.T) {
	NewTesting().
		SetCases([]XzTestingCase{
			{
				Name: "1+2",
				Args: KMap{"a": 1, "b": 2},
				Want: KMap{"res": 3},
			},
			{
				Name: "2+2=4",
				Args: KMap{"a": 2, "b": 2},
				Want: KMap{"res": 4},
			},
		}).
		SetLogic(func(args KMap) KMap {
			res := make(KMap, 0)
			res["res"] = sum(args["a"].(int), args["b"].(int))
			return res
		}).
		Done(t)
}
MIT License Copyright (c) 2023 qingbing Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

通用的go函数包 expand collapse
Go and 2 more languages
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/duqingbing/go-utils.git
git@gitee.com:duqingbing/go-utils.git
duqingbing
go-utils
go-utils
master

Search