【Gin】(二)Gin 路由及路由文件封装

一、环境安装

1、GO语言环境安装

【Golang】(二)Go语言环境安装_安装golang 环境-CSDN博客

2、Gin 介绍与环境搭建

https://blog.csdn.net/forest_long/article/details/139088121

二、路由

1、默认模式

main.go

package main

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

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

	// 此 handler 将匹配 /user/john 但不会匹配 /user/ 或者 /user
	router.GET("/user/:name", func(c *gin.Context) {
		name := c.Param("name")
		c.String(http.StatusOK, "Hello %s", name)
	})

	// 此 handler 将匹配 /user/john/ 和 /user/john/send
	// 如果没有其他路由匹配 /user/john,它将重定向到 /user/john/
	router.GET("/user/:name/*action", func(c *gin.Context) {
		name := c.Param("name")
		action := c.Param("action")
		message := name + " is " + action
		c.String(http.StatusOK, message)
	})

	router.Run(":8080")
}

2、分组模式

package main

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

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

	//默认模式
	server.GET("/hello", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{
			"message": "Hello World22",
		})
	})

	//分组模式
	user := server.Group("/user")
	{
		user.GET("/list", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"message": "user",
			})
		})

		user.POST("/add/:id", func(context *gin.Context) {
			//id := context.Param("id")
			context.String(http.StatusOK, "id:id")
		})
	}

	server.Run(":8080")
}

3、路由文件模式1

main.go

package main

import (
	"gin01/router"
)

func main() {

	server := router.Router()

	server.Run(":8080")
}

router/user.go

package router

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

func Router() *gin.Engine {
	r := gin.Default()

	user := r.Group("/user")
	{
		user.GET("/list", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"message": "user",
			})
		})

		user.POST("/add/:id", func(context *gin.Context) {
			//id := context.Param("id")
			context.String(http.StatusOK, "id:id")
		})
	}

	return r
}

测试:

4、路由文件模式2

main.go

package main

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

func main() {
	// 创建服务
	server := gin.Default()

	// 路由设置
	router.RouterUser(server)
	router.RouterNews(server)

	// 启动服务
	server.Run(":8080")
}

router/user.go

package router

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

func RouterUser(r *gin.Engine) {

	user := r.Group("/user")
	{
		user.GET("/list", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"message": "user",
			})
		})

		user.POST("/add/:id", func(context *gin.Context) {
			//id := context.Param("id")
			context.String(http.StatusOK, "id:id")
		})
	}

}

router/news.go

package router

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

func RouterNews(r *gin.Engine) {

	news := r.Group("/news")
	{
		news.GET("/list", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"message": "news",
			})
		})

		news.POST("/add/:id", func(context *gin.Context) {
			//id := context.Param("id")
			context.String(http.StatusOK, "id:id")
		})
	}

}

推荐使用 路由文件模式2 

测试

http://localhost:8080/news/list

http://localhost:8080/user/list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

forest_long

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

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

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

打赏作者

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

抵扣说明:

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

余额充值