要求

  • Go 1.11 及以上版本 (Go modules 要求)

很快将不再支持Go 1.7或Go 1.8。

安装

要安装 Gin 软件包,需要先安装 Go。

1.下载并安装 gin:

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

2.将 gin 引入到代码中:

import "github.com/gin-gonic/gin"
http.StatusOKnet/http
import "net/http"

使用 Go modules 工具管理项目(Go modules 需要 go v1.11 及以上版本才支持)

cd
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"

2.初始化 go mod

$ go mod init project

3.启动项目

$ go run main.go

开始

不确定如何编写和执行 Go 代码? 点击这里.

example.go
$ touch example.go
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() // 监听并在 0.0.0.0:8080 上启动服务
}
go run example.go
# 运行 example.go 并且在浏览器中访问 0.0.0.0:8080/ping
$ go run example.go