# go-api-template **Repository Path**: dongzi1998/go-api-template ## Basic Information - **Project Name**: go-api-template - **Description**: 基于gin封装的api快速开发框架 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 0 - **Created**: 2021-03-14 - **Last Updated**: 2024-11-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # go-react-admin-api > [前端页面](https://gitee.com/dongzi1998/go-react-admin-web) ## 介绍 golang gin 框架快速开发模板 ## 如何运行 > 最低内存:512MB ### 二进制 ```shell # 拉取代码 git clone git://... # 编译 bash scripts/shell/build.sh linux # 初始化目录 bash scripts/shell/init.sh # 配置文件 vi conf/app_prod.conf # 运行指定配置 ./bin/go-react-admin-api \ --env=prod # 通过env参数选取配置文件 ``` ### Docker ```shell # 构建镜像 docker build -t ggt:v1.0.0 ./ # 启动 docker-compose -f ./scripts/docker/docker-compose-prod up -d ``` ## 三方依赖 - github.com/gin-gonic/gin (Web Framework) - copier (结构体拷贝) - github.com/go-redis/redis/v7 (Redis) - gorm.io/gorm (数据库ORM) - github.com/spf13/viper (配置) - github.com/spf13/pflag (环境变量配置) - go.uber.org/zap (日志) - - gopkg.in/natefinch/lumberjack.v2 (日志切割) - github.com/Masterminds/squirrel (Sql 拼接) - github.com/shirou/gopsutil (系统相关信息 cpu mem) - github.com/robfig/cron/v3 (定时任务) (已集成) - github.com/bluele/gcache (内存缓存) - golang.org/x/sync/singleflight (单执行器,防止缓存击穿) - github.com/dgrijalva/jwt-go (Token令牌工具包) - gopkg.in/gomail.v2 (邮件) - github.com/google/uuid (UUID) ## 项目目录结构 - conf (配置读取、组件初始化) - api (http,rpc 请求相关) - middleware (中间件) - models (数据模型、orm 映射) - routers (路由) - services (API 服务,一般为实现 Requester 接口的方法) - internal (项目私有包) - e (常用常量、变量) - pkg (项目内通用包) - sql (sql 模版) - util (工具包) - job (定时任务) - storage (本地存储相关) - db - cache - logs - tmp - scripts (脚本) - shell - docker - http - proto (protobuf 文件,用来快速生成 restful 接口) ## Live Template ```text # 创建接口(DTO、Requester、Impl)dri type $API$Req struct { } type $API$Resp struct { } func (req *$API$Req) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) { return req.Do(ctx, s) } func (req *$API$Req) Do(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) { var data $API$Resp $Impl$ resp.SetData(data) return } # 创建分页查询结构 ( Query、Count) qac func (req *$API$) query(selects []string, s *service.Service, items *[]$$API$Resp$) (count int) { itemsSq := squirrel.Select(selects...) if items == nil { // 无分组 sql, value, _ := itemsSq.ToSql() s.MblDb.Raw(sql, value...).Scan(&count) // 有分组情况 countSq := squirrel.Select("count(1)") toSql, value, _ := itemsSq.ToSql() countSq = countSq.From(fmt.Sprintf("(%s)t", toSql)) toSql, _, _ = countSq.ToSql() s.MblDb.Raw(toSql, value...).Scan(&count) } else {π if !req.IsExport { if req.PageSize != 0 && req.Page != 0 { itemsSq = itemsSq.Limit(uint64(req.PageSize)).Offset(uint64(req.PageSize * (req.Page - 1))) } } sql, value, _ := itemsSq.ToSql() s.MblDb.Raw(sql, value...).Debug().Scan(items) } return } # 快速创建查询语句(Sql)ss $Result$Sq := squirrel. Select([]string{ "", }...). From(""). LeftJoin(""). Where("") toSql,value,_ := $Result$Sq.ToSql() # 统计方法执行速度(Execution Time) et start := time.Now() defer func() { duration := time.Since(start) fmt.Printf("$1$ Execution time: %v\n", duration) }() ``` ## Live Template 使用示例 ```text selectS := "select *" req.query(selectS, s, &data.Items) data.Total = req.query(selectS, s, nil) ``` ## Restful 处理结构图 ## Protobuf-go 使用详解 ### 安装依赖 ```shell go install google.golang.org/protobuf/cmd/protoc-gen-go@latest ``` ###  使用生成 golang struct ```shell protoc --go_out=. demo.proto # --proto_path # --go_out=out 指定golang文件输出目录 # --go_opt=source_relative 指定写入目录 # paths=source_relative 生成至相对目录 # demo.proto 指定proto文件 # bar/baz.proto ``` ### 创建 proto 文件 ``` option go_package = "go-gin-template/project" ``` ## 常用工具 ### gormt 安装 ```shell go get -u -v github.com/xxjwxc/gormt@latest ``` 配置 ```yaml ``` 生成go代码 ```shell ```