Loading

go语言常用第三方包

GoLang常用第三方扩展

包与下载方式

之前自己写框架的时候经常会用到的第三方包,这里记录一下.

  • goimports: 自动地添加或删除 import 声明
  • easyjson 一个json处理包,比原版性能好很多
  • zap: 非常好用的一个日志包,可以分级
  • sirupsen/logrus: 也是一个带有等级的日志系统,性能不太好
  • xorm: ORM包,非常好用也很完善
  • xorm.io/reverse Xorm的表结构自动同步脚手架
  • gorilla websocket包,对websocket封装的很简便.用起来省劲
  • fatih set: 一个SET包,可以快速获取 并集 交集 差集等
  • httprouter: 很方便的一个http包,比原版封装的更加简便
  • amqp: RabbitMQ包
  • mgutz/str: 切割字符串,匹配字符串之类的很方便
  • gopkg.in/ini.v1: 程序包ini在Go中提供了INI文件读取和写入功能。
  • dgrijalva/jwt-go JWT包
  • beego/session session管理包-beego
  • /go-session/gin-session sessoin管理包-gin
  • appleboy/gin-jwt/v2 jwt-gin
  • /shopspring/decimal 高精度浮点数运算,默认16位精度
  • google/uuid 谷歌官方开源的uuid
  • satori/go.uuid 一个很出名的UUID库
  • boltdb/bolt 将数据存储在本地文件中,类似于一个本地数据库一样
  • jinzhu/copier copy everything
go get -u -v golang.org/x/tools/cmd/goimports
go get -u -v go.uber.org/zap
go get -u -v github.com/go-xorm/xorm
go get -u -v github.com/gorilla/websocket
go get -u -v gopkg.in/fatih/set.v0
go get -u -v github.com/julienschmidt/httprouter
go get -u -v github.com/streadway/amqp
go get -u -v github.com/mgutz/str
go get -u -v github.com/sirupsen/logrus
go get -u -v gopkg.in/ini.v1
go get -u -v github.com/dgrijalva/jwt-go
go get -u -v github.com/astaxie/beego/session
go get -u -v github.com/gin-contrib/sessions
go get -u -v github.com/appleboy/gin-jwt/v2
go get -u -v xorm.io/reverse
go get -u -v github.com/shopspring/decimal	 
go get -u -v github.com/mailru/easyjson
go get -u -v github.com/google/uuid
go get -u -v github.com/satori/go.uuid
go get -u -v github.com/boltdb/bolt
go get -u -v git@github.com:jinzhu/copier.git

xrom的reverse自动生成Model

这里有一个表结构

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `username` char(32) NOT NULL DEFAULT '' COMMENT '自定义账户',
  `mobile` char(15) NOT NULL DEFAULT '' COMMENT '手机号',
  `email` char(32) NOT NULL DEFAULT '' COMMENT '邮箱',
  `wechat_key` varchar(255) NOT NULL DEFAULT '' COMMENT '微信Key',
  `apple_key` varchar(255) NOT NULL DEFAULT '' COMMENT '苹果Key',
  `nickname` char(12) NOT NULL DEFAULT '' COMMENT '昵称',
  `password` char(32) NOT NULL DEFAULT '' COMMENT '登录密码',
  `login_status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '登录状态',
  `created` int(15) NOT NULL DEFAULT '0' COMMENT '创建时间',
  `updated` int(15) NOT NULL DEFAULT '0' COMMENT '更新时间',
  `deleted` int(15) NOT NULL DEFAULT '0' COMMENT '删除时间',
  `created_ip` char(15) NOT NULL DEFAULT '' COMMENT '创建IP',
  `updated_ip` char(15) NOT NULL DEFAULT '' COMMENT '更新IP',
  `version` int(11) NOT NULL DEFAULT '0' COMMENT '版本',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UQE_user_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. 首先准备配置文件lh_custom.yml

这里采用的导出格式是默认配置, 具体需要的配置内容,我已在配置文件中标注

该配置文件,我是放在项目中的docs目录下,所以使用的是../models目录

#  安装
#  go get -u -v xorm.io/reverse
#
#  使用
#  reverse -f example/reverse_models.yml
#
#  模板函数
#  UnTitle: 将单词的第一个字母大写。
#  Upper: 将单词转为全部大写。
#  TableMapper: 将表名转为结构体名的映射函数。
#  ColumnMapper: 将字段名转为结构体成员名的函数。
#
#  模板变量
#  Tables: 所有表。
#  Imports: 所有需要的导入。

kind: reverse
name: mydb
source:
  database: mysql
  conn_str: "root:password@tcp(127.0.0.1:3306)/lh-test?parseTime=true" # dsn
targets:
  - type: codes
    language: golang
    include_tables: # 包含的表,以下可以用 "**"
      - "**"
    exclude_tables: # 排除的表,以下可以用 "**"
      - none
    table_mapper: snake # 表名到代码类或结构体的映射关系
    column_mapper: snake # 字段名到代码或结构体成员的映射关系
    table_prefix: "" # 表前缀
    multiple_files: true # 是否生成多个文件
    table_name: true # 生成模板,如果这里定义了,优先级比 template_path 高
    template: |
      package models

      {{$ilen := len .Imports}}
      {{if gt $ilen 0}}
      import (
        {{range .Imports}}"{{.}}"{{end}}
      )
      {{end}}

      {{range .Tables}}
      type {{TableMapper .Name}} struct {
      {{$table := .}}
      {{range .ColumnsSeq}}{{$col := $table.GetColumn .}}	{{ColumnMapper $col.Name}}	{{Type $col}} `{{Tag $table $col}}`
      {{end}}
      }
      {{end}}
    template_path: ./template/goxorm.tmpl # 生成的模板的路径,优先级比 template 低,但比 language 中的默认模板高,上面已配置,所以这里可以不用管
    output_dir: ../models # 代码生成目录
#  安装
#  go get -u -v xorm.io/reverse
#
#  使用
#  reverse -f example/reverse_models.yml
#
#  模板函数
#  UnTitle: 将单词的第一个字母大写。
#  Upper: 将单词转为全部大写。
#  TableMapper: 将表名转为结构体名的映射函数。
#  ColumnMapper: 将字段名转为结构体成员名的函数。
#
#  模板变量
#  Tables: 所有表。
#  Imports: 所有需要的导入。

kind: reverse
name: mydb
source:
  database: mysql
  conn_str: "root:password@tcp(127.0.0.1:3306)/lh-gin?parseTime=true" # dsn
targets:
  - type: codes
    language: golang
    include_tables: # 包含的表,以下可以用 "**"
      - "**"
    exclude_tables: # 排除的表,以下可以用 "**"
      - none
    table_mapper: snake # 表名到代码类或结构体的映射关系
    column_mapper: snake # 字段名到代码或结构体成员的映射关系
    table_prefix: "" # 表前缀
    multiple_files: true # 是否生成多个文件
    table_name: true # 生成模板,如果这里定义了,优先级比 template_path 高
    template: |
      package models

      {{$ilen := len .Imports}}
      {{if gt $ilen 0}}
      import (
      {{range .Imports}}"{{.}}"{{end}}
      )
      {{end}}

      {{range .Tables}}
      type {{TableMapper .Name}} struct {
      {{$table := .}}
      {{range .ColumnsSeq}}{{$col := $table.GetColumn .}}	{{ColumnMapper $col.Name}}	{{Type $col}} `{{Tag $table $col}}`
      {{end}}
      }

      func (m *{{TableMapper .Name}}) TableName() string {
        return "{{$table.Name}}"
      }
      {{end}}
    template_path: ./template/goxorm.tmpl # 生成的模板的路径,优先级比 template 低,但比 language 中的默认模板高
    output_dir: ../models # 代码生成目录
  1. 下载包
go get -u -v xorm.io/reverse
go install xorm.io/reverse
  1. 生成models

运行命令,生成的model

注意,如果文件已存在会覆盖该文件,同时如果文件中有其他内容,也并不会保留下来.

reverse -f ./lh_custom.yml

user.go

package models

type User struct {
	Id          int    `xorm:"not null pk autoincr comment('主键ID') unique INT(11)"`
	Username    string `xorm:"not null default '' comment('自定义账户') CHAR(32)"`
	Mobile      string `xorm:"not null default '' comment('手机号') CHAR(15)"`
	Email       string `xorm:"not null default '' comment('邮箱') CHAR(32)"`
	WechatKey   string `xorm:"not null default '' comment('微信Key') VARCHAR(255)"`
	AppleKey    string `xorm:"not null default '' comment('苹果Key') VARCHAR(255)"`
	Nickname    string `xorm:"not null default '' comment('昵称') CHAR(12)"`
	Password    string `xorm:"not null default '' comment('登录密码') CHAR(32)"`
	LoginStatus int    `xorm:"not null default 0 comment('登录状态') TINYINT(2)"`
	Create      int    `xorm:"not null default 0 comment('创建时间') INT(15)"`
	Updated     int    `xorm:"not null default 0 comment('更新时间') INT(15)"`
	Deleted     int    `xorm:"not null default 0 comment('删除时间') INT(15)"`
	CreatedIp   string `xorm:"not null default '' comment('创建IP') CHAR(15)"`
	UpdatedIp   string `xorm:"not null default '' comment('更新IP') CHAR(15)"`
	Version     int    `xorm:"not null default 0 comment('版本') INT(11)"`
	Created     int    `xorm:"not null default 0 comment('创建时间') INT(15)"`
}

更加具体的内容,可参考 Xorm的官方

posted @ 2021-07-30 15:40  我爱吃炒鸡  阅读(1227)  评论(0编辑  收藏  举报