一、Gin介绍
Gin是一个用Go编写的HTTPweb框架。它是一个类似于martini但拥有更好性能的API框架, 优于httprouter,速度提高了近 40 倍。点击此处访问Gin官方中文文档。
二、安装
1、安装Gin
go get -u github.com/gin-gonic/gin
2、代码中引入
import "github.com/gin-gonic/gin"
代码
package main
import "github.com/gin-gonic/gin"
func main() {
//go get -u github.com/gin-gonic/gin 执行get拉取包
ginServer := gin.Default() //创建服务
ginServer.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello go"})
})
ginServer.POST("/user", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "创建成功"})
})
ginServer.PUT("/user", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "修改成功"})
})
ginServer.DELETE("/user", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "删除成功"})
})
ginServer.Run(":8082")
}
结果: