dqzd92796 2015-08-18 00:26
浏览 47
已采纳

`go build`不必要地重建

go build and go run are very slow on a tiny program I have (cgo invocations in particular). I'd like go to cache the binary so that it only rebuilds when the source is newer. I would use a simple Makefile with a % rule, but the language designers claim that go's build support doesn't need Makefiles.

Is there another alternative I've overlooked? Does the go community prefer another build system, maybe hash-based instead, for caching and reusing build products?

  • 写回答

2条回答 默认 最新

  • dsaj20411 2015-08-18 04:21
    关注

    I wrote a tool that happens to solve this as a side effect. go build alone will not check if the executable it's producing is already up to date. go install does, and if you tweak it to install to a location of your choice, then you'll get the desired result, similar to go build.

    You can see the behaviour you describe by doing something like this:

    $ go get -d github.com/anacrolix/missinggo/cmd/nop
    $ time go run "$GOPATH"/src/github.com/anacrolix/missinggo/cmd/nop/*.go
    
    real    0m0.176s
    user    0m0.142s
    sys     0m0.048s
    

    That's on a warm run. go run will link on every invocation, just as go build would. Note that github.com/anacrolix/missinggo/cmd/nop is an program that does absolutely nothing.

    Here's invoking the same package, using my tool, godo:

    $ time godo github.com/anacrolix/missinggo/cmd/nop
    
    real    0m0.073s
    user    0m0.029s
    sys     0m0.033s
    

    For larger programs, the difference should be more pronounced.

    So in summary, your standard tooling option is to use go install, or an alternative like godo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线