在一个项目中,我们会有很多路由,那么我们该如何更好的管理自己的路由,在多人协同的情况下可以更好的规范路由呢,我来说一下自己的做法

1.承接gin框架初识(先跑一个简单demo) ①,先创建一个controller文件夹,再创建子文件夹及go文件,如下:

2.每个go文件的代码如下:

project->index.go:

package main

import (
c "project/controller" "github.com/gin-gonic/gin"
) func main() {
r := gin.Default()
rr := c.GinRouter(r) // 监听并在 0.0.0.0:8080 上启动服务
rr.Run(":8080")
}

project->controller->index.go:

package controller

import (
"fmt"
"project/controller/forth"
"project/controller/second"
"project/controller/third" "github.com/gin-gonic/gin"
) func GinRouter(r *gin.Engine) *gin.Engine {
rr := r.Group("/")
rr.GET("/first", func(c *gin.Context) {
fmt.Println("first .........")
})
rr = r.Group("/a")
second.Routers(rr)
third.Routers(rr)
forth.Routers(rr)
return r
}

project->controller->second->index.go:

package second

import (
"fmt" "github.com/gin-gonic/gin"
) func Routers(r *gin.RouterGroup) {
rr := r.Group("")
rr.POST("/second", Function)
return
}
func Function(c *gin.Context) {
fmt.Println("second .........")
return
}

project->controller->third->index.go:

package third

import (
"fmt" "github.com/gin-gonic/gin"
) func Routers(r *gin.RouterGroup) {
rr := r.Group("")
rr.POST("/third", Function)
return
}
func Function(c *gin.Context) {
fmt.Println("third .........")
return
}

project->controller->forth->index.go:

package forth

import (
"fmt" "github.com/gin-gonic/gin"
) func Routers(r *gin.RouterGroup) {
rr := r.Group("")
rr.POST("/forth", Function1)
rr.POST("/five", Function2)
return
}
func Function1(c *gin.Context) {
fmt.Println("forth .........")
return
}
func Function2(c *gin.Context) {
fmt.Println("five .........")
return
}

3.执行一下代码:

这样的写法能够保证,在多人协作的情况下,代码的合并不会出现很大的问题,而且路由管理也相方便。

gin框架封装自己的路由 ②的更多相关文章

  1. Gin框架系列02:路由与参数

    回顾 上一节我们用Gin框架快速搭建了一个GET请求的接口,今天来学习路由和参数的获取. 请求动词 熟悉RESTful的同学应该知道,RESTful是网络应用程序的一种设计风格和开发方式,每一个URI ...

  2. gin框架使用【5.路由分组】

    package mainimport ( "github.com/gin-gonic/gin")func main() { router := gin.Default() v1Ro ...

  3. gin框架使用【3.路由参数】

    GET url: http://127.0.0.1:8080/users/{id} http://127.0.0.1:8080/users/1   对于id值的获取 package main impo ...

  4. gin框架实现一个简单的项目 ③

    承接:gin框架封装自己的路由 ② 对于一个项目来说,需要将各个功能模块分开,也就是所谓的三层模型,这里介绍一下个人的做法: contorller主要负责路由 model主要负责程序输入输出的数据 s ...

  5. gin框架中的路由

    基本路由 gin框架中采用的路由库是基于httrouter做的 地址为:https://github.com/julienschmidt/httprouter httprouter路由库 点击查看代码 ...

  6. gin框架学习手册

    前言 gin框架是go语言的一个框架,框架的github地址是:https://github.com/gin-gonic/gin 转载本文,请标注原文地址:https://www.cnblogs.co ...

  7. GO语言GIN框架入门

    Gin框架介绍 Gin是一个用Go语言编写的web框架.它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter,速度提高了近40倍. 中文文档 Gin框架安装与使用 ...

  8. Gin 框架 - 安装和路由配置

    目录 概述 Gin 安装 路由配置 推荐阅读 概述 看下 Gin 框架的官方介绍: Gin 是一个用 Go (Golang) 编写的 web 框架. 它是一个类似于 martini 但拥有更好性能的 ...

  9. 01 . Go框架之Gin框架从入门到熟悉(路由和上传文件)

    Gin框架简介 Gin是使用Go/Golang语言实现的HTTP Web框架, 接口简洁, 性能极高,截止1.4.0版本,包含测试代码,仅14K, 其中测试代码9K, 也就是说测试源码仅5k左右, 具 ...

随机推荐

  1. 手把手教你MyEclipseUML建模(下)

    手把手教你MyEclipseUML建模(下) 点击访问:手把手教你MyEclipseUML建模(上) 1.用UML 2建模 MyEclipse提供了以下UML 2特性: UML 2图:类.组件.部署. ...

  2. python中关于shutdown 和closesocket的彻底理解!

    关于shutdown 和closesocket的彻底理解! shutdown 和closesocket 来,咱们彻底的来讨论一下这个shutdown 和closesocket 从函数调用上来分析(ms ...

  3. Hibernate 自动更新表出错 建表或添加列,提示标识符无效

    如Oracle 数据库下报错: create table db_meta_web.user (id varchar2(255 char) not null, account varchar2(255 ...

  4. osg机械臂施工模拟

    线程 0x2278 已退出,返回值为 0 (0x0). =====IfcTreeWidget==slotObjectsSelected1IfcObjectAttributeExtraction === ...

  5. Arduino---HC-05 蓝牙模块

    蓝牙基础知识回顾: (一)Arduino和HC-05连接 注意:Arduino通过TX与HC-05进行通信,而Arduino的电压为5V,HC-05的允许电压为3.3V.短时间通信无妨(长时间可能烧毁 ...

  6. C++线程互斥、同步

     一.线程互斥 如果多个线程需要访问且可能修改同一个变量,那么需要加锁,保证同一时刻只有一个线程可以访问,这个动作即最小“原子操作” 方式1: 使用c++提供的类mutex,lock,unlock即可 ...

  7. spring boot入门学习---热部署

    1.maven文件 2.application.properties文件配置

  8. Elasticsearch 7.x文档基本操作(CRUD)

    官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html 1.添加文档 1.1.指定文档ID PUT ...

  9. LeetCode_101. Symmetric Tree

    101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  10. Node.js使用MySQL连接池示例

    下面是一个封装好的工具类: var fs = require('fs'); var mysql = require('mysql'); var pool = mysql.createPool({ ho ...