Imooc gin入门:快速开始

2-1 gin基础:快速开始
更新于: 2022-01-17 15:06:09

安装gin

# 理论步骤
mkdir -p $GOPATH/src/github.com/afeiship/gin_test_project
cd $_
go mod init
go get -v github.com/gin-gonic/gin@v1.4

# 我的实操作
cd /Users/aric.zheng/go/src/github.com/gin-get-started
go mod init
go get -v github.com/gin-gonic/gin@v1.4
❯ tree
.
├── README.md
├── go.mod
├── go.sum
└── start
    └── main.go

1 directory, 4 files

hello gin:

  • start/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()
}
❯ curl localhost:8080/ping
{"message":"pong"}
Hello, Gin 效果图

参考