Go

gin参数校验

May 8, 2024
Go, Gin

第三方包 # github.com/go-playground/validator/v10 常用参数 # required //必填; len=11 //长度=11; min=3 //如果是数字,验证的是数据大小范围,最小值为3,如果是文本,验证的是最小长度为3, max=6 //如果是数字,验证的是数字最大值为6,如果是文本,验证的是最大长度为6 mail //验证邮箱 gt=3 //对于文本就是长度>=3 lt=6 //对于文本就是长度<=6 翻译中间件 # package middleware import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" validator "github.com/go-playground/validator/v10" enTranslations "github.com/go-playground/validator/v10/translations/en" zhTranslations "github.com/go-playground/validator/v10/translations/zh" ) func Translations() gin.HandlerFunc { return func(c *gin.Context) { //locale := .GetHeader("Acept-Language") locale := "zh" uni := ut.New(en.New(), zh.New()) trans, _ := uni.GetTranslator(locale) v, ok := binding.Validator.Engine().(*validator.Validate) if ok { switch locale { case "zh": _ = zhTranslations. ...

gorm自制日志

May 8, 2024
Go, Gorm, Mysql, Log

第三方包 # gorm.io/gorm/logger 示例 # package boot import ( "context" "fmt" "io/ioutil" "log" "os" "time" "github.com/xiaohubai/alpha/config" "go.uber.org/zap" "gorm.io/gorm/logger" "gorm.io/gorm/utils" ) type cfg struct { SlowThreshold time.Duration Colorful bool LogLevel logger.LogLevel } type traceRecorder struct { logger.Interface BeginAt time.Time SQL string RowsAffected int64 Err error } func (t traceRecorder) New() *traceRecorder { return &traceRecorder{Interface: t.Interface, BeginAt: time.Now()} } func (t *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { t. ...

go接口方法重载

May 8, 2024
Go

源码 # package main import "fmt" type Log interface { Error() } type LocalLog struct { Level string `json:"level"` Key string `json:"key"` Msg string `json:"msg"` TraceId string `json:"trace_id"` } type Atta struct { Level string `json:"level"` Key string `json:"key"` Msg string `json:"msg"` TraceId string `json:"trace_id"` AttaId string `json:"atta_id"` } func CommError(obj Log) { obj.Error() } func (l *LocalLog) Error() { fmt.Println(l.Level, l.TraceId) } func (a *Atta) Error() { fmt.Println(a.Level, a. ...

go项目分层设计

May 8, 2024
Go

单系统 Project Layout 介绍 # ├── api (API) │ └── v1 (版本) ├── service (业务层) ├── model (结构体层) ├── dao (数据库操作) ├── middleware (中间件) ├── config (全局配置) ├── boot (初始组件) ├── router (路由控制) ├── plugin (组件具体实现) ├── utils (工具) ├── script (脚本) ├── docs (文档) ├── public (静态文件) ├── log (日志) ├── main.go (入口) ├── go.mod (项目依赖包版本) ├── go.sum (已下载的所有依赖版本) ├── Dockerfile (docker部署文件) └── README.md (项目说明) 大仓微服务 project layout # ├── api //所有proto文件定义,外部引用 │ └── user │ ├── user. ...

zap使用

May 8, 2024
Go, Log, Zap

第三方包 # go.uber.org/zap zap记录日志 # uber 开源的高性能日志库,面向高性能 package boot import ( "fmt" "path" "time" "github.com/xiaohubai/alpha/config" zaprotatelogs "github.com/lestrrat-go/file-rotatelogs" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var level zapcore.Level // Zap 日志组件 func Zap() (logger *zap.Logger) { level = zap.InfoLevel logger = zap.New(getEncoderCore()) logger.WithOptions(zap.AddCaller()) return logger } func getEncoderCore() (core zapcore.Core) { writer, err := GetWriteSyncer() // 使用file-rotatelogs进行日志分割 if err != nil { panic(fmt.Errorf("Get Write Syncer Failed err:%v", err.Error())) } return zapcore.NewCore(zapcore.NewJSONEncoder(getEncoderConfig()), writer, level) } func getEncoderConfig() (cfg zapcore. ...

控制协程运行时间

May 8, 2024
Go

go控制协程运行时间,使用context.WithTimeout(newCtx, timeout),用recover捕获异常情况

...