go语言web开发系列之三十:gin:为路由按版本分组

一,安装用到的库:

1,gin框架在github的地址:

https://github.com/gin-gonic/gin

2,从命令行安装:

root@ku:/data/go/ginhello# go get -u github.com/gin-gonic/[email protected]

说明:刘宏缔的go森林是一个专注golang的博客,
          地址:https://blog.csdn.net/weixin_43881017

说明:作者:刘宏缔 邮箱: [email protected]

二,演示项目的相关信息

1,地址:

https://github.com/liuhongdi/digv30

2,功能:演示按版本号给路由分组

3,项目结构:如图:

go语言web开发系列之三十:gin:为路由按版本分组_第1张图片

三,go代码说明

1,router/router.go

package router

import (
	"github.com/gin-gonic/gin"
	"github.com/liuhongdi/digv30/controller"
	v1 "github.com/liuhongdi/digv30/controller/v1"
	v2 "github.com/liuhongdi/digv30/controller/v2"
	"github.com/liuhongdi/digv30/global"
	"log"
	"runtime/debug"
)

func Router() *gin.Engine {
	router := gin.Default()
	//处理异常
	router.NoRoute(HandleNotFound)
	router.NoMethod(HandleNotFound)
	router.Use(Recover)
	// 路径映射
	//无group
	indexc:=controller.NewIndexController()
	router.GET("/index/index", indexc.Index);
    //v1 group
	apiv1 := router.Group("/v1")
	{
		goodsc:=v1.NewGoodsController()
		apiv1.GET("/goods/one", goodsc.GoodsOne)
		apiv1.GET("/goods/list", goodsc.GoodsList)
		//v1.POST("/read", readEndpoint)
	}
	//v2 group
	apiv2 := router.Group("/v2")
	{
		goodsc:=v2.NewGoodsController()
		apiv2.GET("/goods/one", goodsc.GoodsOne)
		apiv2.GET("/goods/list", goodsc.GoodsList)
	}
	return router
}

func HandleNotFound(c *gin.Context) {
	global.NewResult(c).Error(404,"资源未找到")
	return
}

func Recover(c *gin.Context) {
	defer func() {
		if r := recover(); r != nil {
			//打印错误堆栈信息
			log.Printf("panic: %v\n", r)
			debug.PrintStack()
			global.NewResult(c).Error(500,"服务器内部错误")
		}
	}()
	//加载完 defer recover,继续后续接口调用
	c.Next()
}

2,controller/v1/goodsController.go

package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/liuhongdi/digv30/global"
)

type GoodsController struct{}
func NewGoodsController() GoodsController {
	return GoodsController{}
}
// v1 商品详情
func (g *GoodsController) GoodsOne(c *gin.Context) {
	result := global.NewResult(c)
	result.Success("v1 one");
	return
}
// v1 商品列表
func (g *GoodsController) GoodsList(c *gin.Context) {
	result := global.NewResult(c)
	result.Success("v1 list");
	return
}

3,controller/v2/goodsController.go

package v2

import (
	"github.com/gin-gonic/gin"
	"github.com/liuhongdi/digv30/global"
)

type GoodsController struct{}
func NewGoodsController() GoodsController {
	return GoodsController{}
}
// v2 商品详情
func (g *GoodsController) GoodsOne(c *gin.Context) {
	result := global.NewResult(c)
	result.Success("v2 one");
	return
}
// v2 商品列表
func (g *GoodsController) GoodsList(c *gin.Context) {
	result := global.NewResult(c)
	result.Success("v2 list");
	return
}

4,其他代码可访问github

四,测试效果

1,查看启动时控制台的输出:

[GIN-debug] GET    /index/index              --> github.com/liuhongdi/digv30/controller.(*IndexController).Index-fm (4 handlers)
[GIN-debug] GET    /v1/goods/one             --> github.com/liuhongdi/digv30/controller/v1.(*GoodsController).GoodsOne-fm (4 handlers)
[GIN-debug] GET    /v1/goods/list            --> github.com/liuhongdi/digv30/controller/v1.(*GoodsController).GoodsList-fm (4 handlers)
[GIN-debug] GET    /v2/goods/one             --> github.com/liuhongdi/digv30/controller/v2.(*GoodsController).GoodsOne-fm (4 handlers)
[GIN-debug] GET    /v2/goods/list            --> github.com/liuhongdi/digv30/controller/v2.(*GoodsController).GoodsList-fm (4 handlers)

2,访问url:

http://127.0.0.1:8080/v1/goods/one

返回:

go语言web开发系列之三十:gin:为路由按版本分组_第2张图片

访问url:

http://127.0.0.1:8080/v2/goods/list

返回:

go语言web开发系列之三十:gin:为路由按版本分组_第3张图片

五,查看库的版本:

module github.com/liuhongdi/digv30

go 1.15

require (
	github.com/gin-gonic/gin v1.6.3
)

 

你可能感兴趣的:(用go做web开发,go,golang,路由算法,api,web)