How can I get a value displayed by "go env" from code?

huangapple go评论83阅读模式
英文:

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

GOMODCACHEstd/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.

huangapple
  • 本文由 发表于 2022年10月17日 03:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74090064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定