go.mod 速查表
go mod init module-name
go get github.com/gin-gonic/gin
go mod tidy
go mod download
go list -m all
go outdated
go mod vendor
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPATH=D:\golangmod
| 变量 | 说明 |
|---|
$GOROOT | Go 安装目录 |
$GOPATH | 工作目录 |
$GOBIN | 可执行文件目录 |
$GO111MODULE | 模块开关 (on/off/auto) |
$GOPROXY | 代理地址 |
$GOPRIVATE | 私有仓库(不走代理) |
go install github.com/cosmtrek/air@latest
air
wget https://goframe.org/cli/linux_amd64/gf && chmod +x gf && ./gf install
go install github.com/swaggo/swag/cmd/swag@latest
swag init
| 框架 | 说明 | 链接 |
|---|
| Gin | 高性能 HTTP 框架 | github.com/gin-gonic/gin |
| Echo | 高性能,极简 | github.com/labstack/echo |
| GoFrame | 全功能企业级框架 | github.com/gogf/gf |
| Fiber | 类 Express 风格 | github.com/gofiber/fiber |
| Beego | MVC 全栈框架 | github.com/beego/beego |
| 库 | 用途 | 链接 |
|---|
| Viper | 配置管理 | github.com/spf13/viper |
| Cobra | CLI 工具 | github.com/spf13/cobra |
| GORM | ORM 数据库 | gorm.io/gorm |
| GoRedis | Redis 客户端 | github.com/redis/go-redis |
| Logrus | 日志库 | github.com/sirupsen/logrus |
| Zap | 高性能日志 | go.uber.org/zap |
| Validator | 验证器 | github.com/go-playground/validator |
| JWT | JWT 认证 | github.com/golang-jwt/jwt |
| Casbin | 权限管理 | github.com/casbin/casbin |
| Testify | 测试工具 | github.com/stretchr/testify |
| 库 | 说明 | 链接 |
|---|
| 标准库 | net/http,功能完善 | 内置 |
| Resty | 类 Python requests | github.com/go-resty/resty |
| Gentleman | 插件驱动的 HTTP 客户端 | github.com/h2non/gentleman |
const layout = "2006-01-02 15:04:05"
now := time.Now()
fmt.Println(now.Format(layout))
fmt.Println(now.Format("2006/01/02"))
- 推荐驼峰命名,不用下划线
- 包名小写,简洁有意义
- 接口名:一个方法加
-er 后缀(如 Reader, Writer) - 错误变量:
Err 开头(如 ErrNotFound) - 函数接收者:1-2 个字母(如
u User, r Rectangle)