Gin 响应方式

世上唯一不能复制的是时间,唯一不能重演的是人生,唯一不劳而获的是年龄。该怎么走,过什么样的生活,全凭自己的选择和努力。人生很贵,请别浪费!与智者为伍,与良善者同行。Gin 响应方式,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

响应

1. 字符串方式

r.GET("/user/save", func(ctx *gin.Context) {
    ctx.String(http.StatusOK, "this is a %s", "ms string response")
})

2. JSON方式

r.GET("/user/save", func (ctx *gin.Context) {
    ctx.JSON(http.StatusOK, gin.H{
        "success": true,
    })
})

3. XML方式

type XmlUser struct {
    Id   int64  `xml:"id"`
    Name string `xml:"name"`
}
r.GET("/user/save", func (ctx *gin.Context) {
    u := XmlUser{
        Id:   11,
        Name: "zhangsan",
    }
    ctx.XML(http.StatusOK, u)
})

4. 文件格式

r.GET("/user/save", func (ctx *gin.Context) {
    //ctx.File("./1.png")
    ctx.FileAttachment("./1.png", "2.png")
})

5. 设置http响应头

r.GET("/user/save", func(ctx *gin.Context) {
    ctx.Header("test", "headertest")
})

6. 重定向

r.GET("/user/save", func(ctx *gin.Context) {
    ctx.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
})

7. YAML方式

r.GET("/user/save", func(ctx *gin.Context) {
    ctx.YAML(200, gin.H{"name": "ms", "age": 19})
})

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/235901.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!