当前位置:网站首页>Go language web development series 30: gin: grouping by version for routing
Go language web development series 30: gin: grouping by version for routing
2022-07-03 13:48:00 【Lao Liu, you are so awesome】
One , Libraries used for installation :
1,gin In the framework of github The address of :
https://github.com/gin-gonic/gin
2, Install... From the command line :
[email protected]:/data/go/ginhello# go get -u github.com/gin-gonic/[email protected] explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two , Information about the demonstration project
1, Address :
https://github.com/liuhongdi/digv30
2, function : Demonstrate grouping routes by version number
3, Project structure : Pictured :

3、 ... and ,go Code instructions
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()
// Handling exceptions
router.NoRoute(HandleNotFound)
router.NoMethod(HandleNotFound)
router.Use(Recover)
// Path mapping
// nothing 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," Resource not found ")
return
}
func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
// Print error stack information
log.Printf("panic: %v\n", r)
debug.PrintStack()
global.NewResult(c).Error(500," Server internal error ")
}
}()
// Finished loading defer recover, Continue with subsequent interface calls
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 Goods details
func (g *GoodsController) GoodsOne(c *gin.Context) {
result := global.NewResult(c)
result.Success("v1 one");
return
}
// v1 List of goods
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 Goods details
func (g *GoodsController) GoodsOne(c *gin.Context) {
result := global.NewResult(c)
result.Success("v2 one");
return
}
// v2 List of goods
func (g *GoodsController) GoodsList(c *gin.Context) {
result := global.NewResult(c)
result.Success("v2 list");
return
}
4, Other codes are accessible github
Four , The test results
1, Check the console output at startup :
[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, visit url:
http://127.0.0.1:8080/v1/goods/onereturn :

visit url:
http://127.0.0.1:8080/v2/goods/listreturn :

5、 ... and , View the version of the library :
module github.com/liuhongdi/digv30
go 1.15
require (
github.com/gin-gonic/gin v1.6.3
)
边栏推荐
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- 服务器硬盘冷迁移后网卡无法启动问题
- 实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- Setting up remote links to MySQL on Linux
- Anan's doubts
- Unity Render Streaming通过Js与Unity自定义通讯
- MySQL 数据处理值增删改
- Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
猜你喜欢

Kivy tutorial how to automatically load kV files

Go language unit test 3: go language uses gocovey library to do unit test

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

Today's sleep quality record 77 points

SQL Injection (GET/Select)

Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?

Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu

挡不住了,国产芯片再度突进,部分环节已进到4nm

Unity EmbeddedBrowser浏览器插件事件通讯

The principle of human voice transformer
随机推荐
常见的几种最优化方法Matlab原理和深度分析
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
8 Queen question
Road construction issues
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
Brief analysis of tensorboard visual processing cases
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
Red Hat Satellite 6:更好地管理服务器和云
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
Golang - command line tool Cobra
记录关于银行回调post请求405 问题
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Ubuntu 14.04 下开启PHP错误提示
Anan's doubts
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
User and group command exercises
Father and basketball