网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

GolangGinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染

这篇文章主要讲解了“Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang GinWeb框架之如何掌握XML/JSON/YAML/ProtoBuf等渲染”吧!

为赤城等地区用户提供了全套网页设计制作服务,及赤城网站建设行业解决方案。主营业务为成都网站设计、网站制作、赤城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

XML,JSON,YAML,ProtoBuf等渲染

package main  import (   "github.com/gin-gonic/gin"   "github.com/gin-gonic/gin/testdata/protoexample"   "net/http" )  func main() {   r := gin.Default()    // gin.H is a shortcut for map[string]interface{}   // gin.H对象是一个map映射,键名为字符串类型, 键值是接口,所以可以传递所有的类型   r.GET("/someJSON", func(c *gin.Context) {     c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    r.GET("/moreJSON", func(c *gin.Context) {     // You also can use a struct     var msg struct {       Name    string `json:"user"`       Message string       Number  int     }     msg.Name = "Lena"     msg.Message = "hey"     msg.Number = 123     // Note that msg.Name becomes "user" in the JSON     // Will output  :   {"user": "Lena", "Message": "hey", "Number": 123}      //JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json".     //JSON方法将给定的结构序列化为JSON到响应体, 并设置内容类型Content-Type为:"application/json"     c.JSON(http.StatusOK, msg)   })    r.GET("/someXML", func(c *gin.Context) {     c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    r.GET("/someYAML", func(c *gin.Context) {     c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})   })    //Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.   //Protocol buffers(简称ProtoBuf)是来自Google的一个跨语言,跨平台,用于将结构化数据序列化的可扩展机制,   //详见:https://developers.google.com/protocol-buffers   r.GET("/someProtoBuf", func(c *gin.Context) {     reps := []int64{int64(1), int64(2)}     label := "test"     // The specific definition of protobuf is written in the testdata/protoexample file.     // 使用protoexample.Test这个特别的protobuf结构来定义测试数据     data := &protoexample.Test{       Label: &label,       Reps:  reps,     }     // Note that data becomes binary data in the response  //将data序列化为二进制的响应数据     // Will output protoexample.Test protobuf serialized data     // ProtoBuf serializes the given struct as ProtoBuf into the response body.     // ProtoBuf方法将给定的结构序列化为ProtoBuf响应体     c.ProtoBuf(http.StatusOK, data)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080") }  /* 模拟测试 curl http://localhost:8080/someJSON {"message":"hey","status":200}  curl http://localhost:8080/moreJSON {"user":"Lena","Message":"hey","Number":123}  curl http://localhost:8080/someXML hey200  curl http://localhost:8080/someYAML message: hey status: 200  curl http://localhost:8080/someProtoBuf test */

安全的JSOn

使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头,  这样的死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀.

package main  import (   "github.com/gin-gonic/gin"   "net/http" )  func main() {   r := gin.Default()    // You can also use your own secure json prefix   // 你也可以自定义安全Json的前缀   r.SecureJsonPrefix(")]}',\n")    //使用SecureJSON方法保护Json不被劫持, 如果响应体是一个数组, 该方法会默认添加`while(1)`前缀到响应头,  这样的死循环可以防止后面的代码被恶意执行, 也可以自定义安全JSON的前缀.   r.GET("/someJSON", func(c *gin.Context) {     names := []string{"lena", "austin", "foo"}      //names := map[string]string{     //  "hello": "world",     //}      // Will output  :   while(1);["lena","austin","foo"]     c.SecureJSON(http.StatusOK, names)   })    // Listen and serve on 0.0.0.0:8080   r.Run(":8080") }  /* 模拟请求:curl http://localhost:8080/someJSON )]}', ["lena","austin","foo"]% */

JSONP

使用JSONP可以实现跨域请求数据, 如果请求中有查询字符串参数callback, 则将返回数据作为参数传递给callback值(前端函数名),整体作为一个响应体,返回给前端.

JSONP是服务器与客户端跨源通信的常用方法. 最大特点就是简单适用, 老式浏览器全部支持, 服务器改造非常小, 它的基本思想是: 网页通过添加一个