当前位置:网站首页>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/one
return :
visit url:
http://127.0.0.1:8080/v2/goods/list
return :
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
)
边栏推荐
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- [技術發展-24]:現有物聯網通信技術特點
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- 顺序表(C语言实现)
- 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
- 栈应用(平衡符)
- Static linked list (subscript of array instead of pointer)
- Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
猜你喜欢
MySQL functions and related cases and exercises
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
HALCON联合C#检测表面缺陷——HALCON例程autobahn
logback日志的整理
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
Resource Cost Optimization Practice of R & D team
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
SQL Injection (GET/Select)
随机推荐
IBEM mathematical formula detection data set
Brief analysis of tensorboard visual processing cases
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
SQL Injection (GET/Select)
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
MapReduce实现矩阵乘法–实现代码
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
常见的几种最优化方法Matlab原理和深度分析
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Windos creates Cordova prompt because running scripts is prohibited on this system
[technology development-24]: characteristics of existing IOT communication technology
Leetcode-1175. Prime Arrangements
【被动收入如何挣个一百万】
[556. Next larger element III]
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
The principle of human voice transformer
[how to earn a million passive income]