go build 和 go run 在我的一个小程序上非常慢(特别是 cgo 调用)。我想去缓存二进制文件,以便它只在源更新时重建。我会使用带有 % 规则的简单 Makefile,但语言设计者声称 Go 的构建支持不需要 Makefile。
我是否忽略了另一种选择? Go 社区是否更喜欢另一种构建系统(可能是基于哈希的构建系统)来缓存和重用构建产品?
最佳答案
go build
和 go install
将很快(Go 1.10,2018 年第一季度)变得更快:参见 this thread和 this文档草稿。
The go command now maintains a cache of built packages and other small metadata (CL 68116 and CL 75473). The cache defaults to the operating system-defined user cache directory but can be moved by setting
$GOCACHE
.
Run "go env GOCACHE
" to see the current effective setting. Right now the go command never deletes anything from the cache. If the cache gets too big, run "go clean -cache
" instead of deleting the directory. That command will preserve the cache'slog.txt
file. In a few weeks I'll ask people to post theirlog.txt
files to a Github issue so that we can evaluate cache size management approaches.The main effect of the build cache is that commands like "
go test
" and "go build
" run fast and do incremental builds always, reusing past build steps as aggressively as possible.
You do not have to use "go test -i
" or "go build -i
" or "go install
" just to get fast incremental builds. We will not have to teach new users those workarounds anymore. Everything will just be fast.
请注意,go install
不会安装指定包的依赖项:请参阅“What does go build
build?”。
关于go - `go build` 不必要地重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32061750/