240707_Gin实现Go REST API

Haoliang Tang Lv3

去便利店买了罐鸡尾酒🍸,用Gin来build一个简单的todo-app.

Installation

参考 https://gin-gonic.com/docs/quickstart/

终端随便在哪个目录下: go install github.com/gin-gonic/gin@latest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ go install github.com/gin-gonic/gin@latest
go: downloading github.com/gin-gonic/gin v1.10.0
go: downloading golang.org/x/net v0.25.0
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading github.com/pelletier/go-toml/v2 v2.2.2
go: downloading github.com/ugorji/go/codec v1.2.12
go: downloading google.golang.org/protobuf v1.34.1
go: downloading github.com/go-playground/validator/v10 v10.20.0
go: downloading golang.org/x/sys v0.20.0
go: downloading golang.org/x/text v0.15.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.3
go: downloading github.com/leodido/go-urn v1.4.0
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading golang.org/x/crypto v0.23.0
go: downloading github.com/go-playground/locales v0.14.1
package github.com/gin-gonic/gin is not a main package

然后GOPATH(go env查看)的目录下就会有下载了的源码

/home/hl_tang/go/pkg/mod/github.com目录下确实多了个gin-gonic目录

Set up

创建项目文件夹

1
mkdir todo-app

cd进去

初始化go module

1
2
3
4
$ go mod init example/todo-app
go: creating new go.mod: module example/todo-app
go: to add module requirements and sums:
go mod tidy

然后项目根目录下生成了一个go.mod文件

不初始化go module,而直接开个文件写入代码,go run运行。可能会报错: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory;

使用 go get 来获取和更新依赖包 (其实用go mod tidy也行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ go get github.com/gin-gonic/gin
go: downloading github.com/bytedance/sonic v1.11.6
go: downloading github.com/goccy/go-json v0.10.2
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading golang.org/x/arch v0.8.0
go: downloading github.com/cloudwego/base64x v0.1.4
go: downloading github.com/twitchyliquid64/golang-asm v0.15.1
go: downloading github.com/bytedance/sonic/loader v0.1.1
go: downloading github.com/klauspost/cpuid/v2 v2.2.7
go: downloading github.com/cloudwego/iasm v0.2.0
go: added github.com/bytedance/sonic v1.11.6
go: added github.com/bytedance/sonic/loader v0.1.1
go: added github.com/cloudwego/base64x v0.1.4
go: added github.com/cloudwego/iasm v0.2.0
go: added github.com/gabriel-vasile/mimetype v1.4.3
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.10.0
go: added github.com/go-playground/locales v0.14.1
go: added github.com/go-playground/universal-translator v0.18.1
go: added github.com/go-playground/validator/v10 v10.20.0
go: added github.com/goccy/go-json v0.10.2
go: added github.com/json-iterator/go v1.1.12
go: added github.com/klauspost/cpuid/v2 v2.2.7
go: added github.com/leodido/go-urn v1.4.0
go: added github.com/mattn/go-isatty v0.0.20
go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.2.2
go: added github.com/twitchyliquid64/golang-asm v0.15.1
go: added github.com/ugorji/go/codec v1.2.12
go: added golang.org/x/arch v0.8.0
go: added golang.org/x/crypto v0.23.0
go: added golang.org/x/net v0.25.0
go: added golang.org/x/sys v0.20.0
go: added golang.org/x/text v0.15.0
go: added google.golang.org/protobuf v1.34.1
go: added gopkg.in/yaml.v3 v3.0.1

可以发现go get github.com/gin-gonic/gin时依然下载了一些刚刚go install没有的东西。比如$GOPATH/pkg/mod/github.com目录下又多了个字节的 JSON 序列化/反序列化库bytedance/sonic

然后go: added把依赖信息写入go.mod文件中的require

go get管理依赖,而直接开个文件写入代码,go run运行。可能会报错: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin

但确实不go get,而用go mod tidy也是work的而且可能更好吧

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import "github.com/gin-gonic/gin"

func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
1
2
# run example.go and visit 0.0.0.0:8080/ping on browser
$ go run example.go

本机运行服务器后,本机浏览器访问http://localhost:8080/ping,得到了{"message":"pong"}的响应

Gin导入测试成功

生产环境最好先go build编译

1
2
go build -o main .
./main

⚠️开80端口需要编译后sudo运行,其他框架如echo也如此

RESTful API写CRUD

Gin官网的文档很垃圾,内容编排居然是按字母序的。

Gin文档不如看下载的源码文件夹里的 GOPATH/pkg/mod/github.com/gin-gonic/gin@v1.10.0/docs

参考

https://gin-gonic.com/ Gin官网

Tutorial: Developing a RESTful API with Go and Gin Go语言官网的Documentation

Build a Rest API with GoLang Laith Academy 基于Gin的

Go API Tutorial - Make An API With Go Tech With Tim 很清晰的入门

Creating a JSON CRUD API in Go (Gin/GORM) 上面两个没连数据库;这个要素多点,对package的组织,还有dotenv

  • Title: 240707_Gin实现Go REST API
  • Author: Haoliang Tang
  • Created at : 2024-07-07 00:00:00
  • Updated at : 2025-04-30 00:00:26
  • Link: https://hl-tang.github.io/2024/07/07/240707_Gin实现Go REST API/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
240707_Gin实现Go REST API