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

(四)参考

  • Gin Web Framework
  • Gin框架入门(一)—HTTP请求

你可能感兴趣的:(Go)