GO-Helper 封装库
go 常用辅助函数及方法封装,不断完善中
获取方式
go get -u gitee.com/tym_hmm/go-helper
目录说明
CryptoHelper
加密处理
Library
内部封装函数库
//1.雪花算法
worker, err := NewWorker("节点id")
if err != nil {
fmt.Println(err)
return
}
id := worker.GetId()
LogHelper
日志库
//在main中初始化全局变量
//需加入 gitee.com/tym_hmm/hlog 库
var TymLog = LogHelper.LoggerStd(hlog.Level(hlog.INFO))
MapHelper
map处理
PathHelper
目录文件处理
ReflectHelper
反射处理
ResponseHelper
http统一响应处理 http响应json处理
SliceHelper
切片处理
StringHelper
字符串处理
TimeHelper
时间处理,如时间格式化、时间计算等
TemplateViewHelper | AssetHelper 一起使用
golang 模板渲染
在main中添加
//go:embed Asset
var PublicAsset embed.FS
//初始化打包模板
t:=TemplateViewHelper.NewTemplateView("Asset/template", ".html")
//初始化静态资源
// "/static/" 浏览器访问路径 如: http://xxx.com/static/demo.js
// &PublicAsset 打包资源变量
// "./Asset/source" 本地存放资源目录
assetHandler := AssetHelper.AssetHandler("/static/", &PublicAsset, "./Asset/source")
//首页 控制器使用
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
data := map[string]string{"siteName": ""}
t.TempViewMultiple(&PublicAsset, writer, data, []string{"default", "head", "foot"})
})
YamlHelper
yaml文件读取
type TYaml struct {
Name string `yaml:"name"`
Number int `yaml:"number"`
}
ty := new(TYaml)
yRead := YamlHelper.NewYamlRead()
err := yRead.Parse("./test.yaml", &ty)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", ty)
}
yaml 生成
ty := &TYaml{
Name: "aaaa1",
Number: 12,
}
yamlWrite := YamlHelper.NewYamlWrite()
err:=yamlWrite.Out(ty, "test.yaml")
if err !=nil{
fmt.Print(err)
}
ExceptionHelper
异常捕获, 采集try Catch 模式处理 对golang[ panic | recover ] 进行封装
使用:(注) 在异常捕获时,最后必须执行Exc
, 否则无法正常捕获
//抛出异常
Exception.Throw(Constant.RESULT_TOKEN_ERROR.Code, Constant.RESULT_TOKEN_ERROR.Message, err)
//异常捕获
Exception.Try(func() {
//这里执行逻辑 如果有异常执行抛出
Exception.Throw(Constant.RESULT_TOKEN_ERROR.Code, Constant.RESULT_TOKEN_ERROR.Message, err)
}).Catch(func(exception Exception.Exception) {
//这里捕获异常
}).Exc()
进制和编码换换
- 进制转换
ConvertBinHelper\ConverBinDecimal.go
b := "559781100197515264"
//转换为10进制
ddd := AnyToDecimal(b, 10)
result := DecimalToAny(ddd, 34)
results := AnyToDecimal(result, 10)
t.Logf("data =>%d, =>%s =>%d", ddd, result, results)
- 编码转换
ConvertBinHelper\ConvertString\ConvertStrng.go
#输出为指定编码
str:=ConvertStringEncode("hello,世界", "gbk")
# 指定编码换转
src:="这是utf-8"
str,_:=convertToString(src,"utf-8", "gbk")
PrecisionHelper
整数、浮点数等精度处理, 包含运算精度
PrecisionHelper.Precision
price, err := PrecisionHelper.NewFromString("136.02")
if err != nil {
panic(err)
}
quantity := PrecisionHelper.NewFromInt(3)
fee, _ := PrecisionHelper.NewFromString(".035")
taxRate, _ := PrecisionHelper.NewFromString(".08875")
subtotal := price.Mul(quantity)
preTax := subtotal.Mul(fee.Add(PrecisionHelper.NewFromFloat(1)))
total := preTax.Mul(taxRate.Add(PrecisionHelper.NewFromFloat(1)))
fmt.Println("price:", price) // price: 136.02
fmt.Println("quantity:", quantity) // quantity: 3
fmt.Println("fee:", fee) // fee: 0.035
fmt.Println("taxRate:", taxRate) // taxRate: 0.08875
fmt.Println("Subtotal:", subtotal) // Subtotal: 408.06
fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421
fmt.Println("Taxes:", total.Sub(preTax)) // Taxes: 37.482861375
fmt.Println("Total:", total) // Total: 459.824961375
fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875
fmt.Println(PrecisionHelper.NewFromFloat(123.123123123123).String()) //123.123123123123
fmt.Println(PrecisionHelper.NewFromFloat(.123123123123123).String()) //0.123123123123123
fmt.Println(PrecisionHelper.NewFromFloat(-1e13).String()) //-10000000000000