go语言web框架gin安装(go mod方式)

开门见山,直接上步骤:

  1. 开启go mod
    export GO111MODULE=on
  2. 初始化go mod
    mkdir /Project/ginProject && cd /Project/ginProject
    go mod init ginLog(名字自定义)
  3. 配置 Go modules proxy
    由于国内的网络问题,go mod 在拉取项目时最好开启代理服务,这里使用阿里云提供的服务:
    export GOPROXY=https://mirrors.aliyun.com/goproxy/
  4. 在当前目录下编写代码
    vim main.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
}
  1. 编译代码 go mod 会自动拉取项目中引入的包
    go build main.go
[root@www ginProject]# go build main.go
go: finding github.com/gin-gonic/gin v1.4.0
go: downloading github.com/gin-gonic/gin v1.4.0
go: extracting github.com/gin-gonic/gin v1.4.0
go: finding github.com/golang/protobuf v1.3.1
go: finding github.com/stretchr/testify v1.3.0
go: finding gopkg.in/go-playground/validator.v8 v8.18.2
go: finding gopkg.in/go-playground/assert.v1 v1.2.1
go: finding golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c
go: finding github.com/ugorji/go v1.1.4
...
  1. 运行gin
    ./main

参考链接:https://linkjian.com/post/use-gomod-for-gin/

你可能感兴趣的:(Go)