突然想起有个鸽了快半年的任务没有完成,记得大概是写个能够响应服务的网页。这么拖下去终究不是事,于是我决心现在开始。

众所周知,一个网站由前端和后端组成。于是这里由后端开始。开发选择了Golang,于是框架也得跟上来(你说我为什么不自己写个框架出来?当然是因为菜得离谱),选择了备受好评的Gin。但光是安装,就有些费周折。

第一步,配置Go环境

首先进入https://golang.org/dl/ 来下载安装包(网络原因进不去的话可以选择https://golang.google.cn/dl/

选择Windows版本的安装包下载安装即可。

可在PowerShell中输入

go version

来查看安装的版本

第二步,Gin的安装

官方的项目地址给出了一个简便的安装方法:直接在Powershell中输入以下代码即可:

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

就这样安装完成当然是最好的,但一段时间后,结果大都是这样的(视网络情况而定):

go get: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": dial tcp 172.217.160.81:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这时就需要一些外来的帮助,比如使用代理来进行下载(这里使用的是goproxy.io):

# 配置 GOPROXY 环境变量
$env:GOPROXY = "https://goproxy.io,direct"
# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"

这时再次运行官方给出的安装指令就可以得到如下结果:

go get -u github.com/gin-gonic/gin<br>go: downloading github.com/gin-gonic/gin v1.6.3<br>go: downloading github.com/gin-contrib/sse v0.1.0<br>go: downloading github.com/mattn/go-isatty v0.0.12<br>go: downloading github.com/go-playground/validator/v10 v10.2.0<br>go: downloading github.com/golang/protobuf v1.3.3<br>go: downloading github.com/ugorji/go v1.1.7<br>go: downloading gopkg.in/yaml.v2 v2.2.8<br>go: downloading github.com/json-iterator/go v1.1.9<br>go: downloading github.com/ugorji/go/codec v1.1.7<br>go: downloading golang.org/x/sys v0.0.0-20200116001909-b77594299b42<br>go: downloading github.com/json-iterator/go v1.1.10<br>go: downloading gopkg.in/yaml.v2 v2.4.0<br>go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742<br>go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421<br>go: downloading github.com/go-playground/validator v9.31.0+incompatible<br>go: downloading github.com/go-playground/validator/v10 v10.4.1<br>go: downloading github.com/ugorji/go v1.2.4<br>go: downloading github.com/ugorji/go/codec v1.2.4<br>go: downloading github.com/leodido/go-urn v1.2.0<br>go: downloading github.com/go-playground/universal-translator v0.17.0<br>go: downloading github.com/modern-go/reflect2 v1.0.1<br>go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd<br>go: downloading golang.org/x/sys v0.0.0-20210309074719-68d13333faf2<br>go: downloading github.com/go-playground/locales v0.13.0<br>go: downloading github.com/leodido/go-urn v1.2.1<br>go: downloading github.com/golang/protobuf v1.4.3<br>go: downloading google.golang.org/protobuf v1.23.0<br>go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9<br>go: downloading golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83<br>go: downloading google.golang.org/protobuf v1.25.0

此时Gin框架已经安装完毕。尝试运行官方demo:

//example.go
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 (for windows "localhost:8080")
}
go run example.go

结果会有一行这样的输出:

example.go:3:8: no required module provides package github.com/gin-gonic/gin: working directory is not part of a module

提示找不到gin这一个包,于是我们进入存放example.go的文件夹

#执行:初始化gin包
go mod init gin

执行完毕后目录下会出现一个go.mod文件,此时再次运行,会有如下提示:

example.go:3:8: no required module provides package github.com/gin-gonic/gin; to add it:<br>go get github.com/gin-gonic/gin

仍然无法找到包。于是指定包的位置:

打开生成的go.mod文件,在其中添加

require github.com/gin-gonic/gin latest

latest修改成当前版本也成

再运行

#添加go.sum文件
go mod download github.com/gin-gonic/gin
#补全依赖关系
go mod tidy
#运行
go run exaample.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    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2021/03/12 - 20:59:32 | 404 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/12 - 20:59:32 | 404 |            0s |       127.0.0.1 | GET      "/favicon.ico"
[GIN] 2021/03/12 - 21:00:49 | 200 |       405.8µs |       127.0.0.1 | GET      "/ping"

运行成功

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注