Gin响应数据 c.String() c.JSON() c.JSONP c.XML() c.HTML()
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")
}
本文作者:春游去动物园
本文链接:https://www.cnblogs.com/chunyouqudongwuyuan/p/17969472
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」