Gorm与Herze学习笔记 | 青训营笔记

101 阅读4分钟

这是我参与「第五届青训营 」笔记创作活动的第2天;

前言

开发大项目第一天,同步复习课程学到的Gorm与Herze的协同使用。

本篇笔记将从Gorm和Herze两个模块分别记录本人对两个框架及其所有职责的理解和整理总结,并附以其在本团队项目初始化阶段的使用情况。

Gorm

首先聊聊Gorm是什么。

Gorm是一个已经迭代了十年+的功能强大的ORM框架,在字节内部被广泛使用并且拥有丰富的开源扩展。

一言以概之,Golang里最时髦的ORM框架。

那么ORM是什么?

ORM(Object Relational Mapping)框架采用元数据来描述对象与关系映射的细节,元数据一般采用XML格式,并且存放在专门的对象一映射文件中。简单理解为一种框架的格式

一言以概之,连数据库用的。

(以上对术语与名词的个性化解释是我接触和使用Gorm的第一步,也是我学习一门新的语言、新的技术时通常采取的策略和方法。)

下面摘一些我学习Gorm时的代码:

    // 使用Gorm首先要做的是连接数据库
    // Gorm中连接数据库惯常的做法是传一个dsn
    // dsn通俗来说是一个包含数据库登录信息的字符串
    dsn := "root:xxx@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
	panic("failed to connect database")
    }

然后CRUD,过于ez,不做赘述。

如上是Gorm的一般用法,至于其他骚操作由于本人数据库课程内容多有遗忘,等补完那一块的内容再call back。

Herze

何为Herze?

Herze是字节内部的HTTP框架,参考了其他开源框架的优势,结合字节跳动内部的需求,具有高易用性、高性能、高扩展性特点。

一言以概之,Golang里挂API用的一个时髦的框架。

怎么用呢?

好心的Herze开发者提供了开箱即用的examples,拉取到本地研究研究也就会用了。

如下先沾一些本人到目前为止对Herze的理解与认识。

Herze代码生成后的layout:

.
├── biz                               // business layer, which stores business logic related processes
│   ├── handler                       // store handler file
│   │   ├── user                      // user corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package
│   │   │   |
│   │   │   |__  user_service.go      // the handler file, the user will implement the method defined by the IDL service in this file, it will search for the existing handler in the current file when "update" command, and append a new handler to the end
│   │   └── ping.go                   // ping handler carried by default, used to generate code for quick debugging, no other special meaning
│   ├── model                         // IDL content-related generation code
│   │   └── user                      // hello/example corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package 
│   │         └── user.go             // the product of thriftgo, It contains go code generated from the contents of hello.thrift definition. And it will be regenerated when use "update" command.
│   └── router                        // generated code related to the definition of routes in IDL
│       ├── user                      // hello/example corresponds to the namespace defined in thrift IDL; for protobuf IDL, it corresponds to the last level of go_package
│       │     ├── hello.go            // the route registration code generated for the routes defined in hello.thrift by hz; this file will be regenerated each time the relevant IDL is updated
│       │     └── middleware.go       // default middleware function, hz adds a middleware for each generated route group by default; when updating, it will look for the existing middleware in the current file and append new middleware at the end
│       └── register.go               // call and register the routing definition in each IDL file; when a new IDL is added, the call of its routing registration will be automatically inserted during the update; do not edit
├── go.mod                            // go.mod file, if not specified on the command line, defaults to a relative path to GOPATH as the module name
├── idl                               // user defined IDL, location can be arbitrary
│   └── user.thrift
├── main.go                           // program entry
├── router.go                         // user defined routing methods other than IDL
└── router_gen.go                     // the route registration code generated by hz, for calling user-defined routes and routes generated by hz

很神奇,如上结构是hz工具直接解析thrift文件生成的项目结构,我也打算将此结构直接用于本组团队项目的开发当中。

最快速上手一种技术的方式就是对着官方文档敲一遍标准代码。经过这一步加上我对课上讲解知识的理解和消化,我也算是逐渐掌握这个框架。

后记

说说大项目,我在团队分工中分得了一些前置依赖的开发,为了赶上项目开发进度目前是边学边开发项目,在用中学,在学中用。明天或后天我试着分享其中些许心得。

see u.