这篇文章,讲怎样引入Web框架Gin, 进行Web Api开发
1. 在main.go中引入包import " github .com/gin-gonic/gin"
控制台运行
go mod tidy
输出
go: finding github.com/gin-contrib/sse latest go: finding github.com/ugorji/go/codec latest go: finding github.com/golang/protobuf/proto latest go: finding github.com/stretchr/testify/assert latest go: finding github.com/modern-go/concurrent latest
表示引用成功
2. 开发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
3. 运行 选择菜单:Run -> Debug ‘go build main.go’
输出结果
[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 :80804. 测试
访问
返回
{
"message": "pong"
}
访问成功