Gin(一):Hello

该系列文章正在更新
升级 golang 版本到 1.16, 升级 Gin 环境到 1.7.1, 重构项目目录与命名

💡什么是Gin

Gin 是一个用 Golang 写的 http web 框架。

这是来自 Gin Github 上的描述。

📦开发环境

  • GoLand 2019.2 EAP / Vs Code

  • GoLang 1.16

  • 采用 Go Modules 进行管理

🕐快速入门

GoLand 新建项目的时候,我们选择 Go Modules(vgo) ,填写我们的项目地址和项目名称,我们命名为 GinHello

https://island-hexo.oss-cn-beijing.aliyuncs.com/gin_create_new_project.jpg

点击 Create ,此时 Goland 为我们生成了项目目录,Go 项目的目录永远是那么的简单,比 JavaMaven 或者 Gradle 生成的项目目录简单多了。

1
2
3
GinHello
|
|-go.mod

对,就是一个文件 ,一个 Go module 文件。go modGo 官方引入的一个依赖管理工具。

通过 go mod 文件进行依赖的。

require github.com/gin-gonic/gin v1.4.0

我们把上面的依赖进行添加到 go module 中, goLand 会自动帮我们进行依赖的下载和管理。

当完成依赖的添加,就可以开始写代码了。

新建一个 main.go 文件。

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

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

func main() {
	router := gin.Default()
	router.Run()
}

Gin 只需要两行代码就可以把我们的服务跑起来。

我们只要点击运行,项目便会启动一个 8080 端口,打开浏览器 localhost:8080 我们便可以看到页面上提示出 404 page not found ,这是因为我们的根路由上并没有返回任何结果。同时我们可以在控制台上看到一些打印信息,其中就包括我们刚刚访问根路由的端口。

项目已经启动了,那么如何返回一个接口呢?

通过 routerHandle 进行配置我们返回的参数。

1
2
3
4
5
6
	// 省略代码
	// 添加 Get 请求路由
	router.GET("/", func(context *gin.Context) {
		context.String(http.StatusOK, "hello gin")
	})
	// 省略代码

此时我们重启项目,重新访问页面 localhost:8080,此刻的页面上已经显示了 hello gin

同样,我们还可以进行 POST,PUT,DELETE等请求方式。

单元测试是项目不能缺少的模块,也是保障项目可以正常运行的重要依赖。下面就对 Gin 进行单元测试。

为了方便单元测试,我们首先要对我们的项目进行一下抽取。

新建立一个文件夹叫做 router

建立 go 文件 router.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func SetupRouter() *gin.Engine {
	router := gin.Default()
	// 添加 Get 请求路由
	router.GET("/", func(context *gin.Context) {
		context.String(http.StatusOK, "hello gin")
	})
	return router
}

同时修改 main.go

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

import (
	"GinHello/router"
)

func main() {
	router := router.SetupRouter()
	_ = router.Run()
}

完成了项目测试的初步建立。

router 目录中, golang 的单元测试都是以 _test 结尾,建立 router_test.go 文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package router

import (
	"github.com/stretchr/testify/assert"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestRouter(t *testing.T) {
	router := SetupRouter()
	w := httptest.NewRecorder()
	req, _ := http.NewRequest(http.MethodGet, "/", nil)
	router.ServeHTTP(w, req)
	assert.Equal(t, http.StatusOK, w.Code)
	assert.Equal(t, "hello gin", w.Body.String())
}

通过 assert 进行断言,来判断返回状态码和返回值是否与代码中的值一致。

此时的项目目录为:

1
2
3
4
5
6
7
8
9
GinHello
|
|-router
|  |-router.go
|  |-router_test.go
|
|-main.go
|-go.mod
|-go.sum

运行单元测试,控制台打印出单元测试结果。

— PASS: TestRouter (0.02s)
PASS

✍总结

通过简单的搭建一个 Gin 项目,可以看到 Go 语言搭建一个 Http 服务器很简单,也很方便,零配置即可完成项目并运行起来。

👩‍💻本章节代码

Github

相关内容