概述
Gin是一个golang的微框架,封装比较优雅,API友好。具有快速灵活,容错方便等特点。Gin自身的net/http足够简单,性能也非常不错
Gin下载: https://github.com/gin-gonic/gin
英文文档:https://gin-gonic.com/docs/
安装
go get -u github.com/gin-gonic/gin
测试
-
导包
import "github.com/gin-gonic/gin" import "net/http" //项目中使用了 http.StatusOK
-
步骤
-
注册一个路由器
router := gin.Default()
-
注册路由处理
router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Hello World") })
-
运行(默认是8080端口)
if true{ router.Run() //默认端口:8080 http://localhost }else{ router.Run(":9999") //指端端口:9999 http://localhost:9999 }
-
切换输出的格式
- 返回json格式
func (c *Context) JSON(code int, obj interface{ })
- 返回xml格式
func (c *Context) XML(code int