# go-demo-2025 **Repository Path**: rxbook/go-demo-2025 ## Basic Information - **Project Name**: go-demo-2025 - **Description**: Go语言常用方法和功能模块封装 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://blog.csdn.net/rxbook - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-08 - **Last Updated**: 2025-12-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Go-demo-2025 ## Gin框架入门 ### Gin框架官网 https://gin-gonic.com/zh-cn/ ### 开始使用 ```shell go get -u github.com/gin-gonic/gin ``` 或者直接在go文件中使用: ```go import "github.com/gin-gonic/gin" ``` 然后: ```shell go mod init go-demo-2025 go mod tidy go mod vendor #可选 ``` 基本示例: `quick_start_demo/gin_http_get.go` 运行: `go run quick_start_demo/gin_http_get.go` 浏览器访问 http://127.0.0.1:9090/get?name=zhangsan ![img.png](images/1726651018.png) ## 基本用法(快速练手) ```go //启动 go run main.go ``` 进入 `router/router.go` 查看具体信息. - GET请求: /get?name=zhangsan - POST请求: /post - 重定向到百度: /redirect1 - 重定向到当前服务的/get路由: /redirect2 - CURL请求三方服务: /curl - 不同格式的内容输出: /output?flag=json/xml/yaml ## 配置文件管理 添加依赖 `go get github.com/spf13/viper`,支持 `JSON, TOML, YAML, HCL` 等格式的配置文件。 在项目根目录下面新建 `conf` 目录,然后新建 `application.yml` 文件,此文件需要忽略版本控制. 每次修改后,记得同步修改 `conf/application.yml.demo` 文件,让别人也知道你添加或修改了哪些内容. - 配置初始化: `common/initialization.go` - 数据库配置: 使用 gorm 初始化数据库配置,参考 `common/database.go` 文件 ## 命令行工具: Cobra Cobra是Go的CLI框架。它包含一个用于创建强大的现代CLI应用程序的库和一个用于快速生成基于Cobra的应用程序和命令文件的工具。 简单理解, 类似于 thinkphp 封装的 `php think xxx` 的命令行工具. Cobra 官网: https://cobra.dev/ ### 快速入门 - 安装: `go get github.com/spf13/cobra` - 入口文件: `command.go` - 核心文件: `cmd/cobra.go` ### 基本用法 测试Demo: `command/testCmd.go` 执行: `go run command.go testCmd --paramA 100 --paramB 200 hello meishi crm` 输出: ```go --- test 运行 --- 参数个数: 3 100 200 0=>hello 1=>meishi 2=>crm ``` > 更多参考: https://www.cnblogs.com/niuben/p/13886555.html ## gorm自动生成go结构体代码 ### 使用GEN工具生成 ```shell go run gorm_generate_db_struct.go ``` > 参考: https://segmentfault.com/a/1190000042502370 ### 使用 gentool 生成 Gen Tool 是一个没有依赖关系的二进制文件,可以用来从数据库生成结构 安装gentool: ``` go install gorm.io/gen/tools/gentool@latest ``` 在项目根目录,执行连接的数据库中指定某几张表结构生成数据库model层 ``` gentool -dsn "root:123456@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" -tables "xxx,yyy,zzz" -outPath "./model/query" ``` **说明:使用此方法,如果只指定其中几个表,那么其它已有的表的`gen.go`会被覆盖,目前暂未找到合适的解决方案,因此改为下面的方式:** 修改项目根目录下面的 `gen.tool` 文件,在 `tables : ` 后面追加你想要新增生成的表名,已有的表名不要动!然后在项目根目录执行下面的命令: ``` gentool -c "./gen.tool" ``` > 参考资料: > > https://gorm.io/gen/gen_tool.html > https://insight.xiaoduoai.com/intelligent-frontiers/tech/gorm-use-gentool-tool-to-generate-structure-from-database.html ## 操作Excel ### 使用率高的几个扩展 - excelize (github.com/xuri/excelize/v2) - excelize (github.com/360EntSecGroup-Skylar/excelize) - xlsx (github.com/tealeg/xlsx/v3) - xxhash (github.com/OneOfOne/xxhash) 相关 Excel 开源类库性能对比: https://xuri.me/excelize/zh-hans/performance.html 此处以 github.com/xuri/excelize/v2 为例演示常用的Excel操作方法. 官方中文文档: https://xuri.me/excelize/zh-hans/ - 安装excelize `go get github.com/xuri/excelize/v2` - 创建 Excel 文档, 参考代码: `utils/dbUtil/xorm.go` ### 测试查询数据库并导出数据: ```shell go run command.go userCmd --operate exportData ```