v2版本开始,最新的CLI工具版本功能会随着GoFrame框架的最新版本编译,引入如果本地的CLI工具自动化生成的代码与项目的GoFrame框架版本出现兼容性问题时,建议升级项目框架版本,或者自定义安装旧版本的CLI工具。旧版本CLI工具安装方式参考仓库首页介绍:https://github.com/gogf/gf-cli

重要说明🔥

  • CLI工具提供的代码生成功能,目的是规范化项目代码编写简化项目开发复杂度让开发者能够把精力聚焦于业务逻辑本身
  • CLI工具本身会需要有一定前置的学习和理解成本(尽量理解为什么),但在熟练之后,大家的开发工作将会事半功倍。
  • CLI工具的代码生成功能针对于企业级项目、多成员的团队性项目中收益会非常高。但针对于单人小型项目,开发者可根据个人意愿评估是否选择使用。GoFrame框架本身只是提供了基础组件,采用了组件化的灵活设计,不会对项目代码做严格的要求;但CLI工具会有一定的条框限制,目的是使得团队中每个成员的步调和风格一致,不会使得开发者的代码编写过于随意。

使用方式

$ gf gen -h
USAGE
    gf gen COMMAND [OPTION]

COMMAND
    ctrl        parse api definitions to generate controller/sdk go files
    dao         automatically generate go files for dao/do/entity
    enums       parse go files in current project and generate enums go file
    pb          parse proto files and generate protobuf go files
    pbentity    generate entity message files in protobuf3 format
    service     parse struct and associated functions from packages to generate service go file

DESCRIPTION
    The "gen" command is designed for multiple generating purposes.
    It's currently supporting generating go files for ORM models, protobuf and protobuf entity files.
    Please use "gf gen dao -h" for specified type help.

相关文档







Content Menu

  • No labels

25 Comments

  1. gf gen dao生成代码的时候,遇到int64类型能否指定json转换的时候转化为字符串?每次都要修改这个,要不然js里面读取的时候精度不够,好麻烦。

    1. 如果有定制化的需求,你可以给对应的结构体实现 MarshalJSON() ([]byte, error) 接口即可,例如:

  2. model的这个设计方便统一管理对象

    但外层文件统一一层目录这个设计极容易造成类型名冲突

    自定义的数据结构都放在一层文件夹,也不方便进行版本控制。

    但这层文件还不能删除,因为Dao层还需要引用。

    建议Dao层直接引用mobel的internal内部暴漏的公共对象

    model外层文件可以根据需要自行组织结构

    现在的外层是食之无味,弃之有肉。





    1. 1、model中的internal用于工具自动化生成的数据模型存储,由工具来维护,多次生成会覆盖的,开发者不能修改。

      2、model外层目录采用的同一个包来管理模型,而不是内部再分成多个包,其实想要解决的是之前gen model存在的痛点:

      • 包名太多,不好维护。且在每一层的代码中,包名容易冲突,例如api/service/dao中可能都会存在同样一个包名叫做user,引用时容易混淆,降低开发维护效率。
      • 统一包名可以在一个包名下查找所有的、公开的资源,不用考虑包名命名问题,方便统一维护,减少心智负担。
      • 不同的数据结构之间存在复用的可能(例如api/service/dao的输入输出参数),多个包的话复用比较困难,造成代码冗余,不好维护。

      3、在不同的代码分层中,可以依靠不同的命名规范来区别不同业务的资源,不同的业务资源也可以在同一个包/目录下使用不同的文件来进行管理。


  3. 没有办法设置某个字段不输出json吗?

  4. sqlite 的自增 ID,使用 dao.Info.Insert() 的时候,id 也会被带进去,始终是0,sqlite 会报错,请问有解决方案吗?


    create table info
    (
        id integer
            constraint license_pk
                primary key autoincrement,
        created_at datetime,
        modified_at datetime,
        enabled int,
        content int
    );
    
    INSERT INTO `info`(`id`,`created_at`,`modified_at`,`enabled`,`content`) 
    VALUES(0,'2021-08-11 22:26:49',null,1,'5FC7EC');



    1. 生成一个新的struct,不要id字段

  5. 刚接触2天gf,在学习gf-demos,发现新工具生成的dao内容和demos里的不一样。

    GoFrame CLI Tool v1.17.0, https://goframe.org 
    GoFrame Version: v1.16.5 in current go.mod 
    CLI Installed At: H:\go\path\bin\gf.exe 
    CLI Built Detail:
      Go Version:  go1.15.14
      GF Version:  v1.16.4
      Git Commit:  971ed46f0b9d4dfebd1907cd3ed851cf9e1a5503
      Build Time:  2021-08-10 02:22:48 


    在serveice/user.go中抄了一段类似的代码报错了

    ```

    // 用户邮箱加密码登录,成功返回用户信息,否则返回nil; password应当会md5值字符串
    func (s *userinfoService) SignIn(ctx context.Context, email, password stringerror {
        var user *model.UserInfo
        err := dao.UserInfo.Where("email=? and password=?", email, password).Scan(&user)
        if err != nil {
            return err
        }
        if user == nil {
            return errors.New("账号或密码错误")
        }
        if err := Session.SetUser(ctx, user); err != nil {
            return err
        }
        Context.SetUser(ctx, &model.ContextUser{
            Uid:      uint(user.Uid),
            Wxamp:    user.Wxamp,
            Username: user.Username,
        })
        return nil
    }

    ```

    1. 好像是

      err := dao.UserInfo.Ctx(ctx).Where("email=? and password=?", email, password).Scan(&user)

  6. func (dao *ProductLabelDao) Transaction(ctx context.Context, f func(ctx context.Context, tx *gdb.TX) error) (err error) {
    return dao.Ctx(ctx).Transaction(ctx, f)
    }
    Transaction 提示没有这个方法,不知道是怎么回事。自动生成的代码。是什么原因呀。

  7. 感谢,对于水平切分的数据库,如果只生成同一个group的dao层,只能在业务上来修改配置获取db对象,只是默认dao层保存的group就没什么用了,请问是否有更好的设计方案,初次接触,请指教

  8. type MpuAccessPlatform struct {
    AccessPlatId int64 `orm:"access_plat_id,primary" json:"accessPlatId,omitempty" description:"接入平台ID,本服务统一管理的接入平台ID"`
    AccessPlatRegionalismCode string `orm:"access_plat_regionalism_code" json:"accessPlatRegionalismCode,omitempty" description:"平台所属行政区划代码,参照《中华人名共和国行政区划代码》"`
    AccessPlatSn string `orm:"access_plat_sn" json:"accessPlatSn,omitempty" description:"平台唯一标识"`
    AccessPlatAlias string `orm:"access_plat_alias" json:"accessPlatAlias,omitempty" description:"平台描述"`
    }
    生成的json标签有没有办法配置是否包含omitempty属性.
  9. 郭老师,您好,工具很好用,给一个大大赞;有一个小小的建议生成文件时 能不能给一个创建时间呀?主要是后期看更新的时间是什么时候,方便后期回滚之类的

    // ==========================================================================
    // Code generated by GoFrame CLI tool. DO NOT EDIT. 这里附加一个创建时间
    // ==========================================================================
    1. 可以提个PR

  10. cgo 也弄了,编译也成功了,就是不能gf gen dao一直不能用


    cannot find database driver for specified database type "sqlite", did you misspell type name "sqlite" or forget importing the database driver?
    1. cannot find database driver for specified database type "sqlite", did you misspell type name "sqlite" or forget importing the database driver?


    建议把sqlite完善下,毕竟这个用的也很多


    推荐给你一个不用cgo的sqlite库: 

    github.com/glebarez/sqlite
    1. 这个底层使用的也是github.com/mattn/go-sqlite3库,也是需要CGO的。

      1. 可能是一个误会,有空在仔细看看

    2. 这个库的确不依赖CGO

    3. 我这边也是跟你一样的,sqllite用不了,不知道你解决了没有

  11. 你把这个sqlite加上,同时再搞一个简单图片处理的库上去,水印翻转,验证码之类的

  12. linux系统:

    执行gf gen service生成报错


    haima@haima-PC:/media/haima/34E401CC64DD0E282/site/go/src/gfoframe/df-web-demo1/myapp$ gf version
    GoFrame CLI Tool v2.1.0-rc4, https://goframe.org
    GoFrame Version: cannot find goframe requirement in go.mod
    CLI Installed At: /usr/local/bin/gf
    CLI Built Detail:
      Go Version:  go1.17.10
      GF Version:  v2.1.0-rc4
      Git Commit:  2022-06-01 16:36:40 0639becccc486fda179b81e7a685f67c91b173a4
      Build Time:  2022-06-01 16:34:52
    haima@haima-PC:/media/haima/34E401CC64DD0E282/site/go/src/gfoframe/df-web-demo1/myapp$ gf gen service
    source folder path "internal/logic" does not exist


    手动新建internal/logic后,再次执行不生成文件

    haima@haima-PC:/media/haima/34E401CC64DD0E282/site/go/src/gfoframe/df-web-demo1/myapp$ gf gen service
    done!

    haima@haima-PC:/media/haima/34E401CC64DD0E282/site/go/src/gfoframe/df-web-demo1/myapp$ ll internal/logic/
    总用量 0




  13. 业务模型也可以定义在api里,这样基于业务模型可以自动生成logic和service,后面是否考虑一下增加一个gf gen logic