2數據綁定

json響應

		c.JSON(200, gin.H{
			"message": "pong",
		})

結構體響應

	r.GET("/ping", func(c *gin.Context) {
		var msg struct{
			Name string
			Message string
			Number int
		}
		msg.Name = "root"
		msg.Message = "message"
		msg.Number = 123
		c.JSON(200,msg)
	})

xml

	r.GET("/ping", func(c *gin.Context) {
		c.XML(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
	})

YAML

	r.GET("/ping", func(c *gin.Context) {
		c.YAML(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
	})

protobuf

	r.GET("/ping", func(c *gin.Context) {
		reps := []int64{int64(1), int64(2)}
		label := "test"
		// The specific definition of protobuf is written in the testdata/protoexample file.
		data := &protoexample.Test{
			Label: &label,
			Reps:  reps,
		}
		// Note that data becomes binary data in the response
		// Will output protoexample.Test protobuf serialized data
		c.ProtoBuf(http.StatusOK, data)
	})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章