说明
以下Golang学习路上的Github优秀项目收藏,做一个抛砖引玉,大家有兴趣上官网看看。(持续更新)
项目规范
经典的Golang项目目录结构
project-layout是 Go 应用程序项目的基本布局。它不是核心 Go 开发团队定义的官方标准;然而,它是 Go 生态系统中一组常见的老项目和新项目的布局模式。其中一些模式比其他模式更受欢迎。它还具有许多小的增强,以及对任何足够大的实际应用程序通用的几个支持目录。
.
├── cmd
│ └── server
│ ├── main.go
│ ├── wire.go
│ └── wire_gen.go
├── config
│ └── local.toml
├── internal
│ ├── handler
│ │ ├── handler.go
│ │ └── biz1.go
│ ├── model
│ │ ├── base.go
│ │ └── biz1.go
│ ├── pkg
│ ├── repository
│ │ ├── repository.go
│ │ └── biz1.go
│ ├── server
│ │ └── http.go
│ └── service
│ ├── service.go
│ └── biz1.go
├── pkg
├── test
├── go.mod
└── go.sum
pre-commit
pre-commit管理和维护多语言预提交钩子的框架。
pre-commit-hooks
pre-commit-hooks是一些用于预提交的开箱即用钩子。
添加以下内容到 .pre-commit-config.yaml 文件
fail_fast: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
- id: check-toml
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
pre-commit-golang
pre-commit-golang是一些用于预提交的开箱即用钩子。
fail_fast: false
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
# - id: go-vet
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: validate-toml
# - id: no-go-testing
- id: golangci-lint
- id: go-critic
- id: go-unit-tests
args: [-tags=cse cgo]
- id: go-mod-tidy
框架
Gin
Gin 是一个用 Go 编写的网络框架。它拥有类似于 martini 的 API,由于使用了 httprouter,性能最高可提升 40 倍。如果你需要高性能和高效率,你一定会爱上 Gin。
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
Fiber
Fiber 是一个受 Express 启发的网络框架,它构建在 Fasthttp(Go 的最快 HTTP 引擎)之上。该框架旨在简化开发过程,实现零内存分配和高性能。
package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})
app.Listen(":3000")
}
代码效率
Wire
Wire 是一种代码生成工具,可使用依赖注入技术自动连接组件。组件之间的依赖关系在 Wire 中表示为函数参数,鼓励使用显式初始化而不是全局变量。由于 Wire 的运行不需要运行时状态或反射,因此使用 Wire 编写的代码甚至可以用于手写初始化。
go install github.com/google/wire/cmd/wire@latest
DB
Qmgo
Qmgo 是一款Go语言的MongoDB driver,它基于MongoDB 官方 driver 开发实现,同时使用更易用的接口设计,比如参考mgo (比如mgo的链式调用)。
Qmgo让您以更优雅的姿势使用MongoDB的新特性。
Qmgo是从mgo迁移到新MongoDB driver的第一选择,对代码的改动影响最小。
Qmgo是在官方Driver上包了一层,相对易用,但同时屏蔽了一些原生的功能(如字段级加密),因此请根据项目的实际情况做选择。
工具
Air热重载
Air 是为 Go 应用开发设计的另外一个热重载的命令行工具。只需在你的项目根目录下输入 air,然后把它放在一边,专注于你的代码即可。
注意:该工具与生产环境的热部署无关。
# binary 文件会是在 $(go env GOPATH)/bin/air
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# 或者把它安装在 ./bin/ 路径下
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s
air -v
一键安装到本地环境
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
go-json
go-json 是 与 Go 的 encoding/json 兼容的快速 JSON 编码器/解码器
go get github.com/goccy/go-json
测试
Mock
go-Mock 是 Go 编程语言的模拟框架。它与 Go 的内置测试包集成得很好,但也可用于其他场合。
go install github.com/golang/mock/mockgen@v1.6.0
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
// Assert that Bar() is invoked.
defer ctrl.Finish()
m := NewMockFoo(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
// Anything else will fail.
m.
EXPECT().
Bar(gomock.Eq(99)).
Return(101)
SUT(m)
}