Gin基础:路由

Gin基础:路由

一、Hello world

package main

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

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World")
	})
	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

二、注册

(一)路由注册

package main

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

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World")
	})
	r.GET("/get", func(c *gin.Context) {
		c.String(http.StatusOK, "this is get")
	})
	r.POST("/post", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "this is post",
		})
	})
	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

(二)路由分层

package main

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

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

	userGroup := r.Group("/user")
	// 注册路由/user/login
	userGroup.POST("/login", func(c *gin.Context) {
		c.String(http.StatusOK, "this /user/login")
	})
	// 注册跌幅/user/logout
	userGroup.GET("/logout", func(c *gin.Context) {
		c.String(http.StatusOK, "this /user/logout")
	})

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

三、参数解析

(一)路由匹配

package main

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

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

	r.GET("/news/:id", func(c *gin.Context) {
		id := c.Param("id")
		fmt.Println(id)

		c.String(http.StatusOK, "OK")
	})

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

GoLand HTTP Client工具:

GET http://localhost:8080/news/1
Accept: */*

执行结果

GET http://localhost:8080/news/1

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 14 Jul 2020 07:24:23 GMT
Content-Length: 2

OK

Response code: 200 (OK); Time: 17ms; Content length: 2 bytes

(二)获取路由参数

package main

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

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

	r.GET("/news", func(c *gin.Context) {
		// 带默认值,如果不存在该参数使用设置的默认值
		//page := c.DefaultQuery("page", "1")
		// 不带默认值,如果不存在该参数使用类型默认值
		page := c.Query("page")

		c.String(http.StatusOK, page)
	})

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

GoLand HTTP Client工具:

GET http://localhost:8080/news?page=1
Accept: */*

执行结果:

GET http://localhost:8080/news?page=1

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 14 Jul 2020 07:37:39 GMT
Content-Length: 1

1

(三)post参数

1.urlencoded
package main

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

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

	r.POST("/news/delete", func(c *gin.Context) {
		// 带默认值,如果不存在该参数使用设置的默认值
		//id := c.DefaultPostForm("id", "1")
		// 不带默认值,如果不存在该参数使用类型默认值
		id := c.PostForm("id")

		c.String(http.StatusOK, id)
	})

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

GoLand HTTP Client工具:

POST http://localhost:8080/news/delete
Accept: */*
Content-Type: application/x-www-form-urlencoded

id=1

执行结果:

POST http://localhost:8080/news/delete

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 14 Jul 2020 07:48:18 GMT
Content-Length: 1

1
2.json
package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"strconv"
)

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

	r.POST("/news/delete", func(c *gin.Context) {
		// 获取请求body
		data, err := ioutil.ReadAll(c.Request.Body)
		if err != nil {
			panic(err)
		}

		// 请求body解析成结构体
		temp := struct {
			Id int `json:"id"`
		}{}

		err = json.Unmarshal(data, &temp)
		if err != nil {
			panic(err)
		}

		c.String(http.StatusOK, "id=" + strconv.Itoa(temp.Id))
	})

	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

GoLand HTTP Client工具:

POST http://localhost:8080/news/delete
Accept: */*
Content-Type: application/json

{
  "id": 1
}

执行结果:

POST http://localhost:8080/news/delete

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 14 Jul 2020 08:08:05 GMT
Content-Length: 4

id=1

(四)参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yimtcode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值