Gin响应数据 c.String() c.JSON()  c.JSONP c.XML() c.HTML()
1 c.String()
package main
import (
	"net/http"
	"github.com/gin-gonic/gin"
)
func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "值:%s", "Hhhh")
	})
	r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
2 c.JSON
package main
import (
	"net/http"
	"github.com/gin-gonic/gin"
)
type Article struct {
	Title string `json:"title"`
	Desc  string `json:"desc"`
}
func main() {
	r := gin.Default()
	r.GET("/json", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{
			"success": true,
			"msg":     "成功",
		})
	})
	r.GET("/json1", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"success": true,
			"msg":     "成功",
		})
	})
     /*
    	map[string]interface{} 和 gin.H 是一样的 
    	因为在gin源码中
    	// H is a shortcut for map[string]interface{}
		type H map[string]interface{}
    */
	a := Article{
		Title: "我是一个标题",
		Desc:  "结构体返回",
	}
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(http.StatusOK, a)
	})
	r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
3.c.JSONP (callback=xxxx )主要解决跨域问题
package main
import (
	"net/http"
	"github.com/gin-gonic/gin"
)
type Article struct {
	Title string `json:"title"`
	Desc  string `json:"desc"`
}
func main() {
	r := gin.Default()
	r.GET("/json", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{
			"success": true,
			"msg":     "成功",
		})
	})
	r.GET("/json1", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"success": true,
			"msg":     "成功",
		})
	})
	a := Article{
		Title: "我是一个标题",
		Desc:  "结构体返回",
	}
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(http.StatusOK, a)
	})
	// 响应JSONP请求
	// http://localhost:8000/json3?callback=xxxx 如果没有callback那么个c.JSON没区别
	// xxxx({"title":"我是一个jsonp","desc":"结构体返回"});
	r.GET("/json3", func(c *gin.Context) {
		a := Article{
			Title: "我是一个jsonp",
			Desc:  "结构体返回",
		}
		c.JSONP(http.StatusOK, a)
	})
	r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
4 c.XML
package main
import (
	"net/http"
	"github.com/gin-gonic/gin"
)
type Article struct {
	Title string `json:"title"`
	Desc  string `json:"desc"`
}
func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "值:%s", "Hhhh")
	})
	r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, map[string]interface{}{
			"success": true,
			"msg":     "成功",
		})
	})
	r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
5 c.HTML
package main
import (
	"net/http"
	"github.com/gin-gonic/gin"
)
type Article struct {
	Title string `json:"title"`
	Desc  string `json:"desc"`
}
func main() {
	r := gin.Default()
	r.LoadHTMLGlob("templates/*")
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "值:%s", "Hhhh")
	})
    
	r.GET("/xml", func(c *gin.Context) {
		// r.LoadHTMLGlob("templates/*") 一定要配置这个
		c.HTML(http.StatusOK, "aaa.html", map[string]interface{}{})
	})
	r.Run(":8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}