LeGEC
LeGEC

Reputation: 51850

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

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.

Upvotes: 0

Views: 245

Answers (1)

Pak Uula
Pak Uula

Reputation: 3425

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.

Upvotes: 2

Related Questions