实例1 涉及客户端json、html、xml、yaml数据返回格式
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
engine := gin.Default()
// json输出
engine.GET("json", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"html": "<br>this is json</br>",
})
})
// html输出
engine.GET("html", func(c *gin.Context) {
c.PureJSON(http.StatusOK, gin.H{
"html": "<br>this is html</br>",
})
})
// xml输出
engine.GET("xml", func(c *gin.Context) {
type message struct {
Name string
age int
address string
}
info := message{"admin", 18, "安徽合肥"}
c.XML(http.StatusOK, info)
})
// yaml输出
engine.GET("yaml", func(c *gin.Context) {
c.YAML(http.StatusOK, gin.H{
"name": "ben",
"sex": "男",
})
})
engine.Run(":8092")
}