标签:def get 自己 连接数 bsp pos 项目 package dex
对于一个项目来说,需要将各个功能模块分开,也就是所谓的三层模型,这里介绍一下个人的做法:
contorller主要负责路由
model主要负责程序输入输出的数据
service主要负责数据处理
utils主要负责存放通用的方法,比如连接数据库
代码如下:
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/second" "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) return r }
project->controller->second->index.go
package second import ( "fmt" ms "project/model/second" ss "project/services/second" "github.com/gin-gonic/gin" ) func Routers(r *gin.RouterGroup) { rr := r.Group("") rr.POST("/second", Function) return } func Function(c *gin.Context) { var input ms.Input if err := c.BindJSON(&input); err != nil { fmt.Println(err) } ss.Function(c, input) return }
project->model->second->index.go
package second type Input struct { Id int `view:"id号" json:"id" from:"id"` }
project->services->second->index.go
package second import ( "fmt" ms "project/model/second" "github.com/gin-gonic/gin" ) func Function(c *gin.Context, input ms.Input) { fmt.Println("second .........,input:", input.Id) return }
标签:def get 自己 连接数 bsp pos 项目 package dex
原文地址:https://www.cnblogs.com/ybf-yyj/p/11910902.html