tools

module
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT

README

tools

GitHub Workflow Status Go Report Card GitHub go.mod Go version GitHub tag (latest by date) GitHub Go Dev GitHub last commit GitHub commit activity

tools 是一个 Go 编写的便携工具库,包含一些初始化功能和默认配置功能,同时将日志、配置、分析工具集成到了一起。

  • 随机种子的初始化
  • zap 日志库的初始化
  • viper 默认配置
  • pflag 默认配置
  • gin 默认配置
  • gorm 默认配置

初始化

import (
	_ "github.com/kcgp007/tools/randInit"
	_ "github.com/kcgp007/tools/loggerInit"
	_ "github.com/kcgp007/tools/gormTool"
)

默认配置

viper

type dataConfig struct {
	S string
	I int
	F float64
}

var data = dataConfig{
	S: "aaa",
	I: 10,
	F: 10.5,
}

// 添加配置
configTool.Add(&data)
// 添加配置并自定义配置名
configTool.AddWithKey("data", &data)

pflag

// 添加方法
flagTool.HelpAndVersion()
// 编译时添加参数
go build -trimpath -ldflags "-s -w -X 'github.com/kcgp007/tools/flagTool.version=$(GIT_VERSION)' -X 'github.com/kcgp007/tools/flagTool.goVersion=$(GO_VERSION)' -X 'github.com/kcgp007/tools/flagTool.buildTime=$(BUILD_TIME)'"

gin

gin.SetMode(gin.TestMode)
router := gin.New()
// 添加错误处理和拦截
router.Use(gin.Logger(), ErrorHandler, ExceptionHandler)
// 添加分析工具
pprof.Register(router)
router.GET("/ping", func(c *gin.Context) {
	c.Status(http.StatusOK)
})
router.GET("/error", func(c *gin.Context) {
	c.Error(fmt.Errorf("test error"))
})
router.GET("/exception", func(c *gin.Context) {
	s := ""
	c.String(http.StatusOK, s[:5])
})
router.Run(":8080")

感谢 JetBrains 的支持

JetBrains Logo (Main) logo

GoLand logo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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