Golang常用库
Gin
go get -u github.com/gin-gonic/ginGin-CORS
go get github.com/gin-contrib/corsSlog 日志库
go get github.com/gookit/slogGORM
go get -u gorm.io/gormgo get -u gorm.io/driver/postgres测试,mock,断言库 Github
go get -u github.com/stretchr/testifyORM-Postgres
go get github.com/uptrace/bun@latestSQL迁移
- github.com/golang-migrate/migrate/v4 迁移库
- github.com/golang-migrate/migrate/v4/database/postgres PG 数据库
- github.com/golang-migrate/migrate/v4/source/file sql 文件读取
go get github.com/golang-migrate/migrate/v4go get github.com/golang-migrate/migrate/v4/database/postgresgo get github.com/golang-migrate/migrate/v4/source/fileGO-PG-migrations Go 操作 sql github
go get github.com/go-pg/migrations/v8godotenv 环境变量
go get github.com/joho/godotenvGORM: 本体
go get -u gorm.io/gorm驱动: mysql:
go get -u gorm.io/driver/mysqlpostgres:
go get -u gorm.io/driver/postgresAIR
开发热重启
Go1.16之后
go install github.com/cosmtrek/air@latestGo1.16之前
go get -u github.com/darjun/go-daily-lib/airgin-swagger
https://github.com/swaggo/gin-swagger 与 Gin 结合的swagger
go install github.com/swaggo/swag/cmd/swag@latestgo get -u github.com/swaggo/gin-swaggergo get -u github.com/swaggo/filesswag initpackage main
import ( "github.com/gin-gonic/gin" docs "github.com/go-project-name/docs" swaggerfiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" "net/http")// @BasePath /api/v1
// PingExample godoc// @Summary ping example// @Schemes// @Description do ping// @Tags example// @Accept json// @Produce json// @Success 200 {string} Helloworld// @Router /example/helloworld [get]func Helloworld(g *gin.Context) { g.JSON(http.StatusOK,"helloworld")}
func main() { r := gin.Default() docs.SwaggerInfo.BasePath = "/api/v1" v1 := r.Group("/api/v1") { eg := v1.Group("/example") { eg.GET("/helloworld",Helloworld) } } r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) r.Run(":8080")}swagger
go get -u github.com/go-swagger/go-swagger/cmd/swaggerMac
- 利用
go env GOPATH查看并复制go安装路径
go env GOPATH- 添加
air别名与air的安装路径
vi ~/.zshrcalias air='/Users/art/go/bin/air'- 重新读取
zsh配置
source ~/.zshrcMongoDB
go get -u go.mongodb.org/mongo-driver/bsonviper
读取配置文件
go get github.com/spf13/viperJWT
go get -u github.com/golang-jwt/jwt/v5