# my-golang **Repository Path**: sn-yang/my-golang ## Basic Information - **Project Name**: my-golang - **Description**: 我的 Go 学习仓库 - **Primary Language**: Go - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-13 - **Last Updated**: 2024-06-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 我的系列 - Golang 篇 ## 特性 - [x] Golang [基本语法](./src/lang/basic.py) - [x] Golang [基本语句](./src/lang/statements.py) - [x] Golang [格式化](./src/lang/format.py) - [x] Golang [数据模型](./src/lang/data_model.py) - [x] Golang [协程](./src/lang/coroutines.py) - [x] Golang [数据转换](./src/utils/convertor.py) - [ ] Golang 的常用工具类代码 - [x] [文件功能](./src/utils/file_util.py) - [x] [MySQL 功能](./src/utils/db_client.py) - [x] [Excel 工具类](./src/utils/excel_util.py) - [x] [Request 工具类](./src/utils/request_util.py) - [ ] 字符串、正则表达式工具类 - [ ] 代码生成工具 - [x] 单元测试代码 - [ ] 安装包生成 ## 开发环境 ## 安装 Golang 每年发布两次。 - [官方下载地址(中国)](https://golang.google.cn/dl/) - [官方下载地址](https://golang.org/dl/) - Windows 从官方下载,并安装。 建议使用 WSL2 + Ubuntu 20.04 将 `%GOPATH%\bin` 加到系统变量 `PATH`中 - Linux ```sh sudo tar -C /usr/local -xzf go1.16.6.linux-amd64.tar.gz echo "export GOROOT=/usr/local/go" >> ~/.bashrc echo "export PATH=/\$PATH:/\$GOROOT/bin" >> ~/.bashrc echo "export GOPATH=$HOME/go" >> ~/.bashrc echo "export PATH=/\$PATH:/\$GOPATH/bin" >> ~/.bashrc source ~/.bashrc # need by unit tests sudo apt install -y gcc ``` - MacOS ```sh brew install go ``` ## 配置 go ```sh # 检查版本 go version # 查看go环境变量 go env # 使用国内代理 go env -w GOPROXY=https://goproxy.cn,direct go env -w GOSUMDB=sum.golang.google.cn ``` ## 编辑器 可以使用 vscode, vim 或者 Goland 作为 golang 的开发环境编辑器。 详细请看[这里](https://golang.google.cn/doc/editors)。 这里是基于 vscode。需要安装 - git - vscode - vscode extension: `golang.go` - vscode extension: `code-runner` 在第一次用 vscode 打开一个 golang 目录,`golang.go`插件会自动安装必要的包。 详细请看: [Go for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=golang.go) 注意:这对 WSL 环境,需要在 WSL 环境内部重新安装上树的插件 ## 配置 vscode - 支持 workspace module: 在一个 Folder 里有多个 go 模块 ```json "go.editorContextMenuCommands": { "generateTestForFile": true, "generateTestForPackage": true }, "go.lintTool": "golint", "go.testFlags": [ "-v" ], "go.toolsManagement.autoUpdate": true, ``` - 支持 code runner ```json "code-runner.executorMap": { "go": "cd $dir && go run $fileName", }, ``` ## 常用命令 ```sh # 安装依赖 go get ./... # 删除不需要的依赖 go mod tidy # 编译 go build # 运行 go run . ``` ## 测试 执行 `go test` 命令,它会在 `*_test.go` 中寻找 `test` 测试、`benchmark` 基准 和 `examples` 示例 函数。 测试函数必须以 `TestXXX` 的函数名出现(XXX 为以非小写字母开头),基准函数必须以 `BenchmarkXXX` 的函数名出现,示例函数必须以 `ExampleXXX` 的形式。 ```sh go test ./tests -v ``` ## Swagger Go ```sh # 获取 swag 命令行工具 go get -u github.com/swaggo/swag/cmd/swag # 产生 swag 文档 swag init ``` [Swagger 首页](http://localhost:7001/swagger/index.html) ## 运行 - 从源码项目中运行 ```sh # 运行 swag init && go run . ``` - 进入 Swagger UI 界面 [Swagger 首页](http://localhost:7001/swagger/index.html) ## Go 代码规范 - [GitLab Go 代码规范](https://docs.gitlab.com/ee/development/go_guide/)