学习gin

编程入门 行业动态 更新时间:2024-10-22 21:21:28

学习<a href=https://www.elefans.com/category/jswz/34/1763738.html style=gin"/>

学习gin

文章目录

        • go:generate
        • Viper 读写配置文件
        • ZAP 保存日志
        • 定时任务
        • 创建api
          • model
          • 步骤 1. 创建service
          • 步骤 2. 创建api
          • 步骤 3. 创建router
        • 初始化总路由
        • 启动
        • go-swagger
          • 路由配置
          • swag init
        • test
        • 将嵌套结构定义为指针或对象利弊
        • 结构体嵌套
        • 学习资源

go:generate

//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=,direct
//go:generate go mod tidy
//go:generate go mod download

Viper 读写配置文件
      v := viper.New()v.SetConfigFile("config.yaml")v.SetConfigType("yaml")err := v.ReadInConfig()if err != nil {panic(fmt.Errorf("Fatal error config file: %s \n", err))}if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {fmt.Println(err)}cs := utils.StructToMap(global.GVA_CONFIG)for k, v2 := range cs {v.Set(k, v2)}v.WriteConfig()
ZAP 保存日志

global.GVA_LOG.Error(“获取失败!”, zap.Error(errors.New(“====”)))
global.GVA_LOG.Info("server run success on ", zap.String(“address”, address))

定时任务
    tm := timer.NewTimerTask()_, err := tm.AddTaskByFunc("func", "@every 1s", mockFunc)if err == nil {tm.StartTask("func")println("StartTask=func")}
创建api

model
api 需要调用的api
router 路由
service 业务代码

model

type Userinfo struct {Name string `uri:"name" form:"name" json:"name"`Pwd  string `uri:"pwd" form:"pwd" json:"pwd"`
}
type UserinfoResponse struct {Userinfo example.Userinfo `json:"userinfo"`Token    string           `json:"token"`
}
步骤 1. 创建service
  1. example/exa_api.go
type ExaApiService struct {
}var ExaApiServiceApp = new(ExaApiService)func (exaApi *ExaApiService) Login(u example.Userinfo) string {return fmt.Sprintf("Login2=name=%s password=%s", u.Name, u.Pwd)
}func (exaApi *ExaApiService) GetUserInfo(u example.Userinfo) string {return fmt.Sprintf("GetUserInfo2=name=%s password=%s", u.Name, u.Pwd)
}
  1. example/enter.go

type ServiceGroup struct {ExaApiService
}
  1. enter.go
 type ServiceGroup struct {ExaServiceGroup example.ServiceGroup
}var ServiceGroupApp = new(ServiceGroup)
步骤 2. 创建api
  1. example/exa_api.go
package exampleimport ("github/gin-gonic/gin""github/githubuityu/study_gin_admin/model/common/response""github/githubuityu/study_gin_admin/model/example"
)type ExaApi struct {
}// Login /*
// http://127.0.0.1:8888/exaApi/login
// application/x-www-form-urlencoded或者form-data
func (e *ExaApi) Login(c *gin.Context) {var userinfo example.Userinfoerr := c.ShouldBind(&userinfo)if err != nil {response.FailWithMessage(err.Error(), c)return}response.OkWithMessage(exaApi.Login(userinfo), c)
}// http://127.0.0.1:8888/exaApi/getUserInfo?name=zhangsan&pwd=6666
func (e *ExaApi) GetUserInfo(c *gin.Context) {var userinfo example.Userinfoerr := c.ShouldBind(&userinfo)if err != nil {response.FailWithMessage(err.Error(), c)return}response.OkWithMessage(exaApi.GetUserInfo(userinfo), c)
}// http://127.0.0.1:8888/exaApi/login
//
//	raw {
//	   "name":"zhangsan",
//	   "pwd":"23333"
//	}
func (e *ExaApi) LoginJson(c *gin.Context) {var userinfo example.Userinfoerr := c.ShouldBindJSON(&userinfo)if err != nil {response.FailWithMessage(err.Error(), c)return}response.OkWithMessage(exaApi.Login(userinfo), c)
}// http://127.0.0.1:8888/exaApi/getUserInfoPath/zhangsan/555
func (e *ExaApi) GetUserInfoPath(c *gin.Context) {var userinfo example.Userinfoerr := c.ShouldBindUri(&userinfo)if err != nil {response.FailWithMessage(err.Error(), c)return}response.OkWithMessage(exaApi.GetUserInfo(userinfo), c)
}
  1. example/enter.go

type ApiGroup struct {ExaApi
}var (exaApi = service.ServiceGroupApp.ExaServiceGroup.ExaApiService
)
  1. enter.go
 type ApiGroup struct {ExaApiGroup example.ApiGroup}var ApiGroupApp = new(ApiGroup)
步骤 3. 创建router
  1. example/exa_api.go
type ExaAPi struct {
}func (e *ExaAPi) InitExaAPiRouter(Router *gin.RouterGroup) {exaApiRouter := Router.Group("exaApi").Use(middleware.ExaMiddleware())exaApiRouterWithoutRecord := Router.Group("exaApi")exaApi := v1.ApiGroupApp.ExaApiGroup{exaApiRouter.POST("login", exaApi.Login)        exaApiRouter.POST("loginJson", exaApi.LoginJson) }{exaApiRouterWithoutRecord.GET("getUserInfo", exaApi.GetUserInfo)                  exaApiRouterWithoutRecord.GET("getUserInfoPath/:name/:pwd", exaApi.GetUserInfoPath) }
}
  1. example/enter.go

type RouterGroup struct {ExaAPi
}
  1. enter.go
type RouterGroup struct {ExaApi example.RouterGroup
}var RouterGroupApp = new(RouterGroup)
初始化总路由

func Routers() *gin.Engine {// 设置为发布模式if global.GVA_CONFIG.System.Env == "public" {gin.SetMode(gin.ReleaseMode) //DebugMode ReleaseMode TestMode}Router := gin.New()exaApiRouter := router.RouterGroupApp.ExaApi//PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix){// 健康监测PublicGroup.GET("/health", func(c *gin.Context) {c.JSON(http.StatusOK, "ok")})}exaApiRouter.InitExaAPiRouter(PublicGroup)global.GVA_LOG.Info("router register success")return Router
}
启动

package mainimport ("fmt""github/githubuityu/study_gin_admin/core""github/githubuityu/study_gin_admin/global""github/githubuityu/study_gin_admin/initialize""go.uber/zap""time"
)//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=,direct
//go:generate go mod tidy
//go:generate go mod download
func main() {global.GVA_VP = core.Viper() // 初始化Viperinitialize.OtherInit()global.GVA_LOG = core.Zap() // 初始化zap日志库zap.ReplaceGlobals(global.GVA_LOG)global.GVA_DB = initialize.Gorm() // gorm连接数据库initialize.Timer()core.RunWindowsServer()}
go-swagger

参数类型 query、path、body、header,formData
参数参数类型 string integer number boolean struct

// Login
// @Summary  用户登录
// @Tags     ExaApi
// @Produce   application/json
// @Param name formData string false "名字"
// @Param pwd formData string false "密码" 
// @Success  200   {object}  response.Response{data=exaApiRes.UserinfoResponse,msg=string}  "返回包括用户信息,token,过期时间"
// @Router   /exaApi/login [post]
func (e *ExaApi) Login(c *gin.Context) {var userinfo example.Userinfoerr := c.ShouldBind(&userinfo)if err != nil {response.FailWithMessage(err.Error(), c)return}response.OkWithDetailed(exaApiRes.UserinfoResponse{Userinfo: userinfo,Token:    userinfo.Pwd,}, "登录成功", c)
}
路由配置
 docs.SwaggerInfo.BasePath = global.GVA_CONFIG.System.RouterPrefix
Router.GET(global.GVA_CONFIG.System.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
swag init
test
  1. 创建一个a.go
  2. 创建一个测试文件a_test.go
  3. 创建一个方法方法名以Test开始,参数为(t *testing.T)
//a.go
package utilstype TestA struct {TestB
}type TestB struct {
}func (tb *TestB) GetInfo() string {return "返回数据"
}var ItyuTestA = new(TestA)package utils//a_test.go
import "testing"func TestAaaa(t *testing.T) {print(ItyuTestA.TestB.GetInfo())
}
将嵌套结构定义为指针或对象利弊
  1. 将嵌套结构定义为指针:
    内存效率:使用指针可以提高内存效率,特别是当嵌套结构体很大时。指针只存储结构体的内存地址,而不是复制整个结构体的数据。
    可变性:如果你需要修改父结构中的嵌套结构的数据,使用指针允许你直接修改被引用的对象。
    避免nil值:如果嵌套结构可能有nil值,使用指针可以通过将指针设置为nil来表示值的缺失。

  2. 将嵌套结构定义为对象:
    简单性:直接使用对象可以简化代码,因为不需要处理指针解引用。它可以使代码更易于阅读和理解。
    避免与指针相关的问题:操作指针需要小心处理,以避免空指针异常和内存泄漏。使用对象可以帮助避免此类问题。
    不可变性:如果嵌套结构的数据在父结构中应该被视为不可变的,那么使用对象可以强制这种不可变性并防止无意的修改。

  3. 最终,对嵌套结构使用指针还是对象的选择取决于性能、可变性要求、内存使用和代码简单性等因素。考虑应用程序的具体需求,并根据这些需求做出决定。

结构体嵌套

结构体嵌套在编程中有多种作用和用途,以下是一些常见的用途:

  1. 组合数据:结构体嵌套允许将多个相关的数据字段组合在一起,形成更复杂的数据结构。通过嵌套其他结构体,可以在一个结构体中包含其他结构体的数据,从而形成逻辑上的组合关系。

  2. 嵌套层次:通过结构体嵌套,可以创建多层次的数据结构,形成树状或层次化的关系。这样可以更好地组织和管理数据,使其更具可读性和可维护性。

  3. 代码复用:结构体嵌套可以实现代码的复用。通过将一个结构体嵌套到另一个结构体中,可以共享嵌套结构体的字段和方法,避免重复定义和编写相同的代码。

  4. 组合功能:通过将一个结构体嵌套到另一个结构体中,可以将嵌套结构体的方法和行为组合到外部结构体中,形成更丰富的功能。外部结构体可以调用嵌套结构体的方法,实现更复杂的行为。

  5. 数据模型:结构体嵌套可以用于定义和表示复杂的数据模型。通过将多个相关的数据结构嵌套在一起,可以更好地表示现实世界的实体和关系。

  6. 总的来说,结构体嵌套提供了一种组合和组织数据的机制,可以使代码更具可读性、可维护性和复用性。它是构建复杂数据结构和实现代码组合的重要工具之一。

学习资源

gin-vue-admin

更多推荐

学习gin

本文发布于:2023-12-07 08:03:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1670615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!