在 $GOPATH 目錄下新建一個項(xiàng)目目錄(eg. gin_start), 并在此項(xiàng)目根目錄下初始化 mod:
go mod init gin_start
解決網(wǎng)絡(luò)問題:
export GOPROXY=https://goproxy.io(windows 使用set)
安裝各種包:
go get -v -u github.com/gin-gonic/gin
新建 main.go 寫自己的代碼:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
fmt.Printf("Start servering")
r := gin.Default()
r.GET("/hello", func(c *gin.Context) {
c.JSON(200, gin.H{
"success": true,
"code": 200,
"message": "Hello World",
"data": nil,
})
})
r.Run(":8000")
}
啟動服務(wù): go run main.go
訪問: http://localhost:8000/hello

image.png
參考鏈接: