`go build` rebuilds unnecessarily

后端 未结 2 2035
感动是毒
感动是毒 2020-12-10 21:22

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 new

2条回答
  •  不知归路
    2020-12-10 21:51

    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.

提交回复
热议问题