文章目录
简介
(官方)Gin is a web framework written in Go (Golang). It features a martini-like API with performance that is up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.
官方地址:https://github.com/gin-gonic/gin
中文文档:https://www.kancloud.cn/shuangdeyu/gin_book/949433
注意:使用gin需要Go的版本号为1.6或更高
安装
安装gin
所需的依赖包
go get -u github.com/gin-gonic/gin
在代码中导入
import "github.com/gin-gonic/gin"
使用示例
快速入门
package main
import (
"github.com/gin-gonic/gin"
)
func sayHello(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello Golang!",
})
}
func main() {
// 创建一个默认的路由引擎
r := gin.Default()
// 指定 用户使用GET请求访问 /hello时,执行sayHello这个函数
r.GET("/hello", sayHello)
// 启动服务,默认8080端口,可以指定为9090
r.Run(":9090")
}
不同的请求方式-RESTful API
HTTP协议中四种不同的请求方法
GET
用来获取资源POST
用来新建资源PUT
用来更新资源DELETE
用来删除资源
func main() {
r := gin.Default()
r.GET("/book", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "GET",
})
})
r.POST("/book", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "POST",
})
})
r.PUT("/book", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "PUT",
})
})
r.DELETE("/book", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "DELETE",
})
})
}
返回JSON、XML等格式
XML、JSON、YAML和ProtoBuf 渲染
func main() {
r := gin.Default()
// gin.H 是一个map类型 map[string]interface{}
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) {
// 也可以使用结构体,但是结构体中的字段首字母需要大写,小写则不可导出
// 如下在结构体中加一个tag,可以使得输出的内容为 user
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}
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")
}
获取querystring请求参数
querystring是什么
query string是传递给这个程序的URL的一部分, 通过对它的使用, 可以允许我们从HTTP的client端发送数据给生成web page的应用程序.
GET请求,URL中?后面的即为query string参数,为key-value格式
例如:
https://cn.bing.com/search?q=querystring
示例
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// 获取浏览器发请求携带的 query string 参数
r.GET("/web", func(c *gin.Context) {
// 三种通过query获取请求中携带的 query string 参数
// name := c.Query("query")
// name := c.DefaultQuery("query","somebody")
name, ok := c.GetQuery("query")
if !ok {
// 取不到值情况
name = "somebody"
}
c.JSON(http.StatusOK, gin.H{
"Name": name,
})
})
r.Run(":9090")
}
//请求 http://192.168.166.89:9090/web?query=1233445
//返回 {"Name":"1233445"}
获取form表单参数
login.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>