Golang环境变量存储在哪里?

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

Where are the golang environment variables stored?

问题

我刚开始在Windows 7上学习golang。

通过使用go env命令,我得到了以下结果:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\Workbench\Go
set GORACE=
set GOROOT=C:\DevTools\Go
set GOTOOLDIR=C:\DevTools\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

然后我在命令行中使用echo %envVar%进行检查,我发现了像以下这样的环境变量:

GOPATH
GOROOT

但其他的环境变量没有找到。

那么它们存储在哪里呢?是一些隐藏的配置文件吗?

英文:

I am just started learning golang on Windows 7.

With go env, I got this:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\Workbench\Go
set GORACE=
set GOROOT=C:\DevTools\Go
set GOTOOLDIR=C:\DevTools\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

Then I checked with echo %envVar% in the command line, I found env vars like:

GOPATH
GOROOT

But others are not found.

So where are they stored? Some hidden configuration file?

答案1

得分: 16

在go1.13中,自定义的GOENV存储在GOENV中,该变量由系统环境变量指定。如果未指定,则将使用适用于您平台的默认值。

要获取您平台的默认位置,请使用go env GOENV:在Linux上,您将得到$HOME/.config/go/env,在macOS上,您将得到$HOME/Library/Application Support/go/env

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/leonardo/go/bin"
GOCACHE="/home/leonardo/.cache/go-build"
GOENV="/home/leonardo/.config/go/env" # 如果系统环境变量'GOENV'为空,则将使用适用于您平台的默认值。
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/leonardo/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct" # 默认值为"https://proxy.golang.org,direc"
$ cat ~/.config/go/env
GOPROXY=https://goproxy.cn,direct

在此文件中以"KEY=VALUE"形式定义的环境变量将覆盖在https://github.com/golang/go/blob/57662b1575030aa09043cd7a48425abdc6e0e0a3/src/cmd/go/internal/cfg/cfg.go#L229中硬编码的默认值。

英文:

In go1.13, customized GOENVs are stored in GOENV, which is specified by system environment variable. If not specified, the default value for your platform will be used.

To obtain default location for your platform, use go env GOENV: on Linux you'll get $HOME/.config/go/env, and on macOS you'll get $HOME/Library/Application Support/go/env.

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/leonardo/go/bin"
GOCACHE="/home/leonardo/.cache/go-build"
GOENV="/home/leonardo/.config/go/env" # if system environment 'GOENV' is empty, the default value for your platform will be used.
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/leonardo/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct" # default value is "https://proxy.golang.org,direc"
$ cat ~/.config/go/env
GOPROXY=https://goproxy.cn,direct

environment variables defined with form "KEY=VALUE" in this file will override the default value hard coded at https://github.com/golang/go/blob/57662b1575030aa09043cd7a48425abdc6e0e0a3/src/cmd/go/internal/cfg/cfg.go#L229

答案2

得分: 10

这些只是你的平台的默认设置。可以将它们视为存储在go.exe可执行文件本身中的设置。

你可以通过将它们设置为其他值来覆盖它们,就像其他环境变量一样。

英文:

Those are just the defaults for your platform. Think of them as stored in the go.exe executable itself.

You can override them by setting them to something else like any other env var.

答案3

得分: 6

像@David Budworth所说的那样,这些变量是您平台的默认值。

大多数情况下,我们会更改$GOPATH$GOROOT变量。例如,在我的电脑上,我使用export GOPATH=/home/user/go,而您的可能与我的不同。

否则,如果您想找到其他变量存储的位置,您需要查看/usr/lib/go-1.6/src(抱歉,我现在正在使用Ubuntu,带有go 1.6,我不知道Windows中go的路径目录),您将在那里找到许多在构建go可执行文件时使用的bash脚本

例如:

在文件:make.bash中,您会看到:在那里声明了$GROOT并用于构建最终的go可执行文件

export GOROOT="$(cd .. && pwd)"

我还看到了您关于GOTOOLDIR以及它如何知道您的计算机上go的安装位置的评论。我想说,go的源代码中有答案,您可以在这里找到它:

// ToolDir is the directory containing build tools.
var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)

编辑:
我找到了这篇关于构建go的好文章:GO如何使用自身进行构建

**附言:**对不起,我的英语不好。我不是以英语为母语的人。

英文:

Like what @David Budworth says, those variables are the defaults for your platform.

Most of case we change $GOPATH and $GOROOT variables. For example in my PC i use export GOPATH=/home/user/go and yours may be different from mine.

Otherwise, if you look to find where other variables are stored you need to look at /usr/lib/go-1.6/src (sorry i'm using Ubuntu right now with go 1.6 and i don't know the path directory of go in Windows), you'll find there many bash scripts used when you built your go executable.

For example:

in the file: make.bash you'll see: $GROOT was declared there and used to build the final go executable:

export GOROOT="$(cd .. && pwd)"

I saw, also, your comments about GOTOOLDIR and how it knows your installation location of go in your box. I would say, that the source code of go have the answer and you can find it here:

// ToolDir is the directory containing build tools.
var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)

Edit:
I found this good article about building go: How GO uses to build itself

PS: Sorry for my english. I'm not a native english speaker.

答案4

得分: 6

请在命令提示符中输入以下命令:

> go env GOENV
/root/.config/go/env
英文:

Please type the following command to your command prompt

> go env GOENV
/root/.config/go/env

答案5

得分: 0

默认情况下,env_file 在 macOS 上不存在。

env_file: $HOME/Library/Application Support/go/env

执行命令 "go env -w $HOME/go" 后,env_file 被创建。

英文:

by default, env_file does not exist on mac_os.

env_file: $HOME/Library/Application Support/go/env

after doing "go env -w $HOME/go", the env_file was created.

huangapple
  • 本文由 发表于 2016年11月27日 11:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/40825613.html
匿名

发表评论

匿名网友

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

确定