51 lines
1006 B
Go
51 lines
1006 B
Go
|
package main
|
|||
|
|
|||
|
import (
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
"github.com/gin-gonic/gin/testdata/protoexample"
|
|||
|
)
|
|||
|
|
|||
|
var r = gin.Default()
|
|||
|
|
|||
|
// 多种响应方式
|
|||
|
func main() {
|
|||
|
//1.JSON
|
|||
|
r.GET("/someJSON", func(c *gin.Context) {
|
|||
|
c.JSON(200, gin.H{"someJson": true})
|
|||
|
})
|
|||
|
//2.结构体响应
|
|||
|
r.GET("/someStruct", func(c *gin.Context) {
|
|||
|
var msg struct {
|
|||
|
Name string
|
|||
|
Massage string
|
|||
|
Num int
|
|||
|
}
|
|||
|
msg.Name = "hello"
|
|||
|
msg.Num = 100
|
|||
|
msg.Massage = "root"
|
|||
|
c.JSON(200, &msg)
|
|||
|
|
|||
|
})
|
|||
|
//3.XML
|
|||
|
r.GET("/someXml", func(c *gin.Context) {
|
|||
|
c.XML(200, gin.H{"someXml": true})
|
|||
|
})
|
|||
|
//4.YAML响应
|
|||
|
r.GET("/someYaml", func(c *gin.Context) {
|
|||
|
c.YAML(200, gin.H{"someYaml": true})
|
|||
|
})
|
|||
|
//5.protobuf格式,谷歌开发的高效存储读取的工具
|
|||
|
r.GET("/someProtobuf", func(c *gin.Context) {
|
|||
|
reps := []int64{int64(1), int64(2)}
|
|||
|
//定义数据
|
|||
|
label := "label"
|
|||
|
//传protobuf格式数据
|
|||
|
data := &protoexample.Test{
|
|||
|
Label: &label,
|
|||
|
Reps: reps,
|
|||
|
}
|
|||
|
c.ProtoBuf(200, data)
|
|||
|
})
|
|||
|
r.Run(":8000")
|
|||
|
}
|