1. 渲染 XML/JSON/YAML/ProtoBuf
func main() {
r := gin.Default()
// gin.H is a shortcut for map[string]interface{}
r.GET("/someJSON", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello", "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 = "wohu"
msg.Message = "hello"
msg.Number = 123
// Note that msg.Name becomes "user" in the JSON
// Will output : {"user": "wohu", "Message": "hello", "Number": 123}
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})
})
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.
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)
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
输出结果:
wohu@wohu-pc:~$ curl http://127.0.0.1:8080/someJSON
{"message":"hello","status":200}
wohu@wohu-pc:~$ curl http://127.0.0.1:8080/moreJSON
{"user":"wohu","Message":"hello","Number":123}
wohu@wohu-pc:~$ curl http://127.0.0.1:8080/someXML
<map><message>hey</message><status>200</status></map>
wohu@wohu-pc:~$ curl http://127.0.0.1:8080/someYAML
message: hey
status: 200
wohu@wohu-pc:~$
2. 上传单个文件
The filename is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done.
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.POST("/upload", func(c *gin.Context) {
// single file
file, _ := c.FormFile("file")
log.Println(file.Filename)
// Upload the file to specific dst.
dst := "test.png"
c.SaveUploadedFile(file, dst)
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
router.Run(":8080")
}
使用方法
curl -X POST http://localhost:8080/upload \
-F "file=@/Users/appleboy/test.zip" \
-H "Content-Type: multipart/form-data"
3. 上传多个文件
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.POST("/upload", func(c *gin.Context) {
// Multipart form
form, _ := c.MultipartForm()
files := form.File["upload[]"]
for _, file := range files {
log.Println(file.Filename)
// Upload the file to specific dst.
c.SaveUploadedFile(file, dst)
}
c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
})
router.Run(":8080")
}
使用方法:
curl -X POST http://localhost:8080/upload \
-F "upload[]=@/Users/appleboy/test1.zip" \
-F "upload[]=@/Users/appleboy/test2.zip" \
-H "Content-Type: multipart/form-data"
4. 替换 JSON 编译
Gin使用 encoding/json作为默认的 json包,但可以通过从其他标签构建来改变它。
$ go build -tags=jsoniter .
$ go build -tags=go_json .
5. 无 MsgPack 渲染功能编译
Gin默认启用 MsgPack渲染功能。但可以通过指定 nomsgpack build标签来禁用这一功能。
$ go build -tags=nomsgpack .
这对于减少可执行文件的二进制大小很有用。
版权声明:本文为wohu1104原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。