英文:
How can I get a value displayed by "go env" from code?
问题
我想要在本地机器上探索模块缓存,并访问go env GOMODCACHE。
是否有一个API可以访问与"go env"生成的相同值,使用当前版本的go编译器编译我的二进制文件时?
我在标准库的go/*包下没有找到相关的包或类型。
英文:
I would like to explore the module cache on local machine, and access go env GOMODCACHE.
Is there an API to access the same values as those generated by "go env", using the current version of my go compiler when I compile my binary ?
I couldn't find a relevant package or type under the go/* packages from the standard library.
答案1
得分: 2
GOMODCACHE 在 std/cmd/go/internal/cfg/ 中被定义。
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
其中 gopathDir 是:
func gopathDir(rel string) string {
list := filepath.SplitList(BuildContext.GOPATH)
if len(list) == 0 || list[0] == "" {
return ""
}
return filepath.Join(list[0], rel)
}
这两个配置项和 gopathDir 都是内部使用的,没有公开的 API。
英文:
GOMODCACHE is defined in std/cmd/go/internal/cfg/
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
wgere gopathDir is
func gopathDir(rel string) string {
list := filepath.SplitList(BuildContext.GOPATH)
if len(list) == 0 || list[0] == "" {
return ""
}
return filepath.Join(list[0], rel)
}
Both configuration and gopathDir are internal. No API exposes it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论