2、gin 路由

分别使用GET、POST、PUT、PATCH、DELETE和OPTIONS访问方式
package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	// 默认路由
	router := gin.Default()

	// get 请求
	router.GET("/appGet", func(c *gin.Context) {
		c.JSON(200, gin.H {
			"Name": "Hello Gin !",
		})
	})
	
	// post 请求
	router.POST("/appPost", func(c *gin.Context) {
		c.String(200, "this is Post! ")
	})

	// Put 请求
	router.PUT("appPut", func(c *gin.Context) {
		c.String(200, "this is Put")
	})

	// Delete 请求
	router.DELETE("/appDelete", func(c *gin.Context) {
		c.String(200, "this is Delete")
	})

	router.Run(":8000")
}

执行结果
╰─$ curl 127.0.0.1:8000/appGet
{"Name":"Hello Gin !"}

╰─$ curl -X POST 127.0.0.1:8000/appPost
this is Post!

╰─$ curl -X PUT 127.0.0.1:8000/appPut
this is Put%

╰─$ curl -X DELETE 127.0.0.1:8000/appDelete
this is Delete%

带参数的路由

1)使用gin.Context中的Param方法解析
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)


// 处理带参数的 path-GET
func getParameter(c *gin.Context) {
	// 获取传入的参数
	Name := c.Param("Name")
	Age := c.Param("Age")

	c.String(http.StatusOK, "name: %s \n age: %s", Name, Age)
} 

// 处理带参数的 path-POST
func postParrameter(c *gin.Context) {
	// 获取传入的参数
	Name := c.Param("Name")
	Age := c.Param("Age")
	c.String(http.StatusOK,  "name: %s \n age: %s", Name, Age)
}

// 注意':'和'*'的区别
func optionalParrameter(c *gin.Context) {
	// 获取传入的参数
	Name := c.Param("Name")
	Age := c.Param("Age")
	c.String(http.StatusOK,  "name: %s \n age: %s", Name, Age)
}

func main() {
	router := gin.Default()

	router.GET("/Get/:Name/:Age", getParameter)
	router.POST("/Post/:Name/:Age", postParrameter)
	// 注意':'必须要匹配,'*'选择匹配,即存在就匹配,否则可以不考虑
	router.GET("/optional/:Name/*Age", optionalParrameter)

	router.Run(":8000")
}


############  执行结果  ############
Get方式请求:http://127.0.0.1:8000/Get/Name=vic/Age=20
返回:
name: Name=vic 
 age: Age=20

Post方式请求:http://127.0.0.1:8000/Post/Name=vic/Age=20
返回:
name: Name=vic 
 age: Age=20

可选参数:
Get方式请求:http://127.0.0.1:8000/Optional/Name=vic/Age=20
返回:
name: Name=vic 
 age: /Age=20

http://127.0.0.1:8000/Optional/Name=vic
返回:
name: Name=vic 
 age: /
2)使用gin.Context中的Query方法解析,类似于正常的URL中的参数传递
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)


// 使用Query获取参数
func getParameter(c *gin.Context) {
	// 获取传入的参数
	Name := c.Query("Name")
	Age := c.Query("Age")

	c.String(http.StatusOK, "name: %s \n age: %s", Name, Age)
} 

// 使用Query获取参数
func postParrameter(c *gin.Context) {
	// 获取传入的参数
	Name := c.Query("Name")
	Age := c.Query("Age")
	c.String(http.StatusOK,  "name: %s \n age: %s", Name, Age)
}


func main() {
	router := gin.Default()
	// 使用Query获取参数
	router.GET("/Get", getParameter)
	router.POST("/Post", postParrameter)

	router.Run(":8000")
}


############  执行结果  ############
Get方式请求
http://127.0.0.1:8000/Get?Name=vic&Age=20

返回
name: vic 
 age: 20


Post方式请求
http://127.0.0.1:8000/Post?Name=vic&Age=20

返回
name: vic 
 age: 20
3)使用gin.Context中的PostForm方法解析
我们需要将参数放在请求的Body中传递, 而不是URL中

package main

import (
	// "net/http"
	"github.com/gin-gonic/gin"
)


// 参数是form中获得,即从Body中获得,忽略URL中的参数
func postParameter(c *gin.Context) {
	// 获取传入的参数
	Message := c.PostForm("message")
	Name := c.PostForm("name")

	c.JSON(200, gin.H{
		"status": "postParamater:posted",
		"message": Message,
		"name": Name,
	})
} 



func main() {
	router := gin.Default()
	// 使用post_form 形式,必须要设置 Post 的 type
	// 同时此方法忽略 URL 中的参数,所有参数需要从 Body 中获得
	router.POST("/Post", postParameter)

	router.Run(":8000")
}
由于我们使用了request Body,那么就需要指定Body中数据的形式,此处是form格式,即application/x-www-form-urlencoded。常见的几种http提交数据方式有: application/x-www-form-urlencoded; multipart/form-data; application/json; text/xml
执行结果

image

路由分组

package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)


func main() {
	router := gin.Default()
	// g1
	g1 := router.Group("/g1")
	{
		g1.GET("/login", func(c *gin.Context) {
			c.String(http.StatusOK, "/g1/login OK! ")
		})
		g1.GET("/read", func(c *gin.Context) {
			c.String(http.StatusOK, "/g1/read OK! ")
		})
	}

	// g2
	g2 := router.Group("/g2")
	{
		g2.POST("/login", func(c *gin.Context) {
			c.String(http.StatusOK, "/g2/login OK! ")
		})
		g2.POST("/submit", func(c *gin.Context) {
			c.String(http.StatusOK, "/g2/submit OK! ")
		})
	}

	router.Run(":8000")
}



### gett执行结果### 
Get方式请求
http://0.0.0.0:8000/g1/login
返回
/g1/login OK

Post执行结果

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值