Gin 特性
- 快速:路由不使用反射,基于Radix树,内存占用少。
- 中间件:HTTP请求,可先经过一系列中间件处理,例如:Logger,Authorization,GZIP等。这个特性和 NodeJs 的 Koa 框架很像。中间件机制也极大地提高了框架的可扩展性。
- 异常处理:服务始终可用,不会宕机。Gin 可以捕获 panic,并恢复。而且有极为便利的机制处理HTTP请求过程中发生的错误。
- JSON:Gin可以解析并验证请求的JSON。这个特性对Restful API的开发尤其有用。
- 路由分组:例如将需要授权和不需要授权的API分组,不同版本的API分组。而且分组可嵌套,且性能不受影响。
- 渲染内置:原生支持JSON,XML和HTML的渲染。
1. 安装,参考网上的教程
1
|
go get -u -v github.com/ramya-rao-a/go-outline
|
提示
1
2
3
|
github.com/ramya-rao-a/go-outline (download)
created GOPATH=/Users/kayoon/go; see 'go help gopath'
package golang.org/x/tools/go/buildutil: unrecognized import path "golang.org/x/tools/go/buildutil" (https fetch: Get https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 142.251.42.241:443: i/o timeout)
|
看情况是被墙了,开了代理,重新试了一下,正常下载
2. 安装完毕
电脑是mac,去用户目录/User/用户名/,看了下多了一个文件夹“go”,里面有/src/github.com/,包含了gin框架的一些组件
1
2
3
4
5
6
7
8
|
(base) \u@\h \w \[\]\[\]\[\]$ cd github.com
(base) \u@\h \w \[\]\[\]\[\]$ ll
total 0
drwxr-xr-x 3 kayoon staff 102B 4 28 11:49 gin-contrib
drwxr-xr-x 3 kayoon staff 102B 4 28 11:49 gin-gonic
drwxr-xr-x 3 kayoon staff 102B 4 28 11:50 go-playground
drwxr-xr-x 2 kayoon staff 68B 4 28 11:51 leodido
drwxr-xr-x 3 kayoon staff 102B 4 28 11:08 ramya-rao-a
|
3. 创建测试项目
1
2
3
|
mkdir gotest
cd gotest
vim gintest.go
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
//1.创建路由
r := gin.Default()
//2.绑定路由规则,执行的函数
r.GET("/", func(context *gin.Context) {
context.String(http.StatusOK, "Hello World!")
})
//3.监听端口,默认8080
r.Run(":8080")
}
|
4. 尝试运行
1
2
|
go run gintest.go
# gintest.go:5:8: no required module provides package github.com/gin-gonic/gin: working directory is not part of a module
|
看了下是没有正常加载包,网上提到可以通过go mod来管理,我用的是1.16版本的go,module默认是开启的
1.13以下版本需要手动开启
1
2
|
go env -w GO111MODULE="on"
go env -w GOPROXY="https://goproxy.io"
|
1
2
3
4
|
go mod init
go: creating new go.mod: module testproject
go: to add module requirements and sums:
go mod tidy
|
当前目录生成了go.mod文件
1
|
go mod edit -require github.com/gin-gonic/gin@latest # 解决,指定Gin的版本
|
1
2
3
|
go mod tidy #引用项目需要的依赖增加到go.mod文件。去掉go.mod文件中项目不需要的依赖。
go: errors parsing go.mod:
/Users/kayoon/go/src/testproject/go.mod:5: require github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": dial tcp 142.251.43.17:443: i/o timeout
|
看起来还是代理的问题,直接切换阿里源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
go mod tidy
# 执行结果
go: downloading github.com/gin-gonic/gin v1.7.7
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/stretchr/testify v1.4.0
go: downloading github.com/json-iterator/go v1.1.9
go: downloading github.com/ugorji/go v1.1.7
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading github.com/go-playground/validator/v10 v10.4.1
go: downloading golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading github.com/go-playground/universal-translator v0.17.0
go: downloading github.com/leodido/go-urn v1.2.0
go: downloading github.com/go-playground/assert/v2 v2.0.1
go: downloading github.com/go-playground/locales v0.13.0
|
5. 启动成功
1
2
3
4
5
6
7
8
9
10
11
12
13
|
go run firstgin.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2022/04/28 - 16:01:11 | 200 | 74.362µs | 127.0.0.1 | GET "/"
[GIN] 2022/04/28 - 16:01:11 | 404 | 531ns | 127.0.0.1 | GET "/favicon.ico"
|