希曼日记

Golang常用库

本页目录 11
  1. Gin
  2. Gin-CORS
  3. ORM-Postgres
  4. SQL迁移
  5. godotenv 环境变量
  6. AIR
  7. gin-swagger
  8. swagger
  9. MongoDB
  10. viper
  11. JWT

Gin

Shell
go get -u github.com/gin-gonic/gin

Gin-CORS

Shell
go get github.com/gin-contrib/cors

Slog 日志库

Shell
go get github.com/gookit/slog

GORM

Shell
go get -u gorm.io/gorm
go get -u gorm.io/driver/postgres

测试,mock,断言库 Github

Shell
go get -u github.com/stretchr/testify

ORM-Postgres

Github

Shell
go get github.com/uptrace/bun@latest

SQL迁移

github

  1. github.com/golang-migrate/migrate/v4 迁移库
  2. github.com/golang-migrate/migrate/v4/database/postgres PG 数据库
  3. github.com/golang-migrate/migrate/v4/source/file sql 文件读取
Text
go get github.com/golang-migrate/migrate/v4
go get github.com/golang-migrate/migrate/v4/database/postgres
go get github.com/golang-migrate/migrate/v4/source/file

GO-PG-migrations Go 操作 sql github

Shell
go get github.com/go-pg/migrations/v8

godotenv 环境变量

Shell
go get github.com/joho/godotenv

GORM: 本体

Shell
go get -u gorm.io/gorm

驱动: mysql:

Shell
go get -u gorm.io/driver/mysql

postgres:

Shell
go get -u gorm.io/driver/postgres

AIR

开发热重启

Go1.16之后

Shell
go install github.com/cosmtrek/air@latest

Go1.16之前

Shell
go get -u github.com/darjun/go-daily-lib/air

gin-swagger

https://github.com/swaggo/gin-swagger 与 Gin 结合的swagger

Shell
go install github.com/swaggo/swag/cmd/swag@latest
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
Shell
swag init
Go
package 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

Shell
go get -u github.com/go-swagger/go-swagger/cmd/swagger

Mac

  1. 利用go env GOPATH查看并复制go安装路径
Shell
go env GOPATH
  1. 添加air别名与air的安装路径
Shell
vi ~/.zshrc
alias air='/Users/art/go/bin/air'
  1. 重新读取zsh配置
Shell
source ~/.zshrc

MongoDB

Shell
go get -u go.mongodb.org/mongo-driver/bson

viper

读取配置文件

Shell
go get github.com/spf13/viper

JWT

Shell
go get -u github.com/golang-jwt/jwt/v5