duancong2160 2019-09-02 12:50
浏览 66

如何只为更改的模板运行go generate?

We use some code templates in our repo, and we rely on go:generate to generate all the needed codes from these templates.
Because there are several templates and from each template we generate several actual codes, consistency of generated codes has become an issue: we needed a command to run and be sure that all generated codes are up to date with their corresponding templates. To do this, we have a simple bash command that find all files that have go:generate in them, and runs go generate in their directory.
The problem is that this approach is really slow: most of the templates have not changed, so there is no need to generate their codes again.
Is there any way to tell go generate to only run if the template is newer than the file? Or, is there any better approach other than this?
Thank you very much.

  • 写回答

1条回答 默认 最新

  • duancan1900 2019-09-02 14:05
    关注

    First, you don't need that bash script that looks for go:generate in files, the go tool itself can do that for you. Just use

    go generate ./...
    

    to recurse in subfolders.

    Next, when a code generation runs, I would store the template file's last modification timestamp in the generated code, either in a comment or in a const / var. The app that is run by go generate could first check if the template's last mod time is newer that the stored one in the code, and only proceed if so.

    This file last mod time should be enough. But if for some reason you can't rely on it, the template could have a "version", which should be incremented when the template is changed. And have the generator "transfer" this version into the final code, which can be used to check if the template is newer than the code generated from it.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂