gpt4 book ai didi

go - 已安装的软件包和GOCACHE有什么区别

转载 作者:行者123 更新时间:2023-12-01 19:59:22 26 4
gpt4 key购买 nike

我始终使用-i标志进行构建以安装软件包,并且.a文件安装在GOPATH/pkg目录中。go env GOCACHE显示的GOCACHE目录似乎也存储高速缓存文件。
它们之间有什么区别?

我想知道,如果我想缩短构建时间,应该同时保存这两个文件吗?

最佳答案

TLDR; 缓存文件夹在go工具内部,其工作对用户来说应该是不透明的,其目的是加快构建和测试的速度。例如,如果您使用版本控制系统(例如git),在分支或版本之间切换,则GOPATH/pkg可能仅包含一个版本的软件包文件。 go缓存文件夹可能包含(部分)多个分支和版本的已编译软件包,当您在分支和版本之间切换时,可以加快将来的构建速度。

缓存文件夹是在Go 1.10中引入的:

The go build command now maintains a cache of recently built packages, separate from the installed packages in $GOROOT/pkg or $GOPATH/pkg. The effect of the cache should be to speed builds that do not explicitly install packages or when switching between different copies of source code (for example, when changing back and forth between different branches in a version control system). The old advice to add the -i flag for speed, as in go build -i or go test -i, is no longer necessary: builds run just as fast without -i. For more details, see go help cache.



因此,您不再需要使用 -i进行快速构建。
go help cache输出的一些引号:

The go command caches build outputs for reuse in future builds. The default location for cache data is a subdirectory named go-build in the standard user cache directory for the current operating system. Setting the GOCACHE environment variable overrides this default, and running 'go env GOCACHE' prints the current cache directory.

The go command periodically deletes cached data that has not been used recently. Running 'go clean -cache' deletes all cached data.

The build cache correctly accounts for changes to Go source files, compilers, compiler options, and so on: cleaning the cache explicitly should not be necessary in typical use. However, the build cache does not detect changes to C libraries imported with cgo. If you have made changes to the C libraries on your system, you will need to clean the cache explicitly or else use the -a build flag (see 'go help build') to force rebuilding of packages that depend on the updated C libraries.

The go command also caches successful package test results. See 'go help test' for details. Running 'go clean -testcache' removes all cached test results (but not cached build results).



缓存文件夹还用于存储测试结果,因此在某些情况下,可以显示缓存的结果而无需再次运行测试。

关于go - 已安装的软件包和GOCACHE有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57863793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com