Skip to main content

Golang常用库

Gin

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

Gin-CORS

go get github.com/gin-contrib/cors

Slog 日志库

go get github.com/gookit/slog

GORM

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

测试,mock,断言库 Github

go get -u github.com/stretchr/testify

ORM-Postgres

Github

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 文件读取
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

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

godotenv 环境变量

go get github.com/joho/godotenv

GORM: 本体

go get -u gorm.io/gorm

驱动: mysql:

go get -u gorm.io/driver/mysql

postgres:

go get -u gorm.io/driver/postgres

AIR

开发热重启

Go1.16之后

go install github.com/cosmtrek/air@latest

Go1.16之前

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

gin-swagger

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

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

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

Mac

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

MongoDB

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

viper

读取配置文件

go get github.com/spf13/viper

JWT

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