栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Gin框架搭建Golang接口项目模板--controller层

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gin框架搭建Golang接口项目模板--controller层

对于刚开始学习Go语言的开发人员,可能急需要入门实际开发,这样才能知道自己学习的知识能干什么,这里我选用Gin框架搭建一个简单的Golang接口项目,降低大家的学习成本。

版本要求:Golang 1.13版本以上,在这里补充一点小知识,由于国内网络环境的影响,需要修改一下Go语言的代理和mod配置,如下图所示:

第一步:使用“go env”查看环境信息

这里注意GO111MODULE和GOPROXY两个配置信息,我这里是修改过了,具体修改方法参见https://github.com/goproxy/goproxy.cn/,按照自己电脑执行命令即可。

  • 创建项目

这里使用的开发工具是Goland,如果你使用其他开发工具没有影响,下面开始创建项目,使用module,如图所示:

创建之后项目结构如下图:

按照上述步骤创建了一个Go语言的空项目,现在按照自己实际项目创建项目结构,这里我简单创建一个,如下图:

  • Gin框架引入

在自己项目路径下执行下面命令:

go get -u github.com/gin-gonic/gin

执行之后再go.mod文件可以看到项目引入的模块信息:

和每一门开发语言一样,Go语言也有自己的入口main,下面在main函数中写项目启动入口:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.New()

	r.GET("/getUser", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"userName":  "WINQI",
			"telephone": "13718768976",
		})
	})

	r.Run()
}

启动测试项目接口:

测试接口:

  • 接口路由

在实际开发中,接口的路由是非常多的,所以这里我们对Gin的路由做了简单的封装,如下:

package route

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func PathRoute(r *gin.Engine) *gin.Engine {

	rootPath := r.Group("/gin")
	{
		userPath := rootPath.Group("/user")
		{
			fmt.Println(userPath)
		}
	}

	return r
}

对main中的代码做一点调整:

package main

import (
	"gin_template/src/common/route"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.New()

	r = route.PathRoute(r)

	r.Run()
}
  • 返回体构造

对于有其他语言开发经验的开发人员来说,应该都有对返回结构体定义的实际经验,这样做可以给请求方统一的返回体,这里我们定义一个简单的返回体:

package response

import (
	"gin_template/src/common/constant"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ResultVO struct {
	Code    constant.ResponseCode `json:"code"`
	Msg     constant.ResponseMsg  `json:"msg"`
	Success bool                  `json:"success"`
	Data    interface{}           `json:"data"`
}


func Success(ctx *gin.Context, code constant.ResponseCode, msg constant.ResponseMsg, data interface{}) {
	resp := &ResultVO{Code: code, Msg: msg, Success: true, Data: data}
	ctx.JSON(http.StatusOK, resp)
}


func Failure(ctx *gin.Context, code constant.ResponseCode, msg constant.ResponseMsg, data interface{}) {
	resp := &ResultVO{Code: code, Msg: msg, Success: false, Data: data}
	ctx.JSON(http.StatusInternalServerError, resp)
}

我们知道在实际开发中会对返回码和返回提示信息做统一的定义,在Go语言中,可以按照如下方式定义(例如:在上面返回体中的constant.ResponseCode):

package constant

type ResponseCode int
type ResponseMsg string

const (
	SelectSuccessCode ResponseCode = 2005
	SelectSuccessMsg  ResponseMsg  = "查询成功"
)

上面简单的对返回体进行了封装,后续会对返回码信息做维护。

  • controller层代码编写

下面是controller层代码,该层代码是最终返回给用户的数据体:

package controller

import (
	"gin_template/src/common/constant"
	"gin_template/src/common/response"
	"github.com/gin-gonic/gin"
)

type UserController struct {

}

func UserRegister(userGrp *gin.RouterGroup) {
	userController := &UserController{}
	userGrp.Use().POST("/findUser", userController.findUser)
}

func (c UserController) findUser(ctx *gin.Context) {

	response.Success(ctx, constant.SelectSuccessCode, constant.SelectSuccessMsg, gin.H{"userName": "月影"})
}

编写完controller后,需要对路由部分的代码做简单的调整:

package route

import (
	"gin_template/src/controller"
	"github.com/gin-gonic/gin"
)

func PathRoute(r *gin.Engine) *gin.Engine {

	rootPath := r.Group("/gin")
	{
		userPath := rootPath.Group("/user")
		{
			controller.UserRegister(userPath)
		}
	}

	return r
}

启动项目测试:

到这里Gin的接口项目创建就完成了,至于持久层(ORM)的选择,可以根据自己的团队情况选择,由于篇幅问题,本节就不对持久层做介绍了,下节会对持久层接入做一个简单的介绍。

项目Github地址:https://github.com/WXCTX007/golang.git

想了解更多内容请关注公众号:编程之艺术

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910007.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号