这篇文章,讲怎样引入Web框架Gin, 进行Web Api开发
1. 在main.go中引入包import "github.com/gin-gonic/gin"
控制台运行
go mod tidy
输出
go: finding github.com/gin-contrib/sse latestgo: finding github.com/ugorji/go/codec latestgo: finding github.com/golang/protobuf/proto latestgo: finding github.com/stretchr/testify/assert latestgo: 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:80803. 运行
选择菜单: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. 测试
访问http://127.0.0.1:8080/ping
返回
{ "message": "pong"}访问成功