17

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?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482

5 Answers5

16

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

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
Liu Hao
  • 511
  • 5
  • 10
10

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.

David Budworth
  • 11,248
  • 1
  • 36
  • 45
  • But for the `GOTOOLDIR`, if it is in the go.exe, how can it know the installation location of go on my box? Or it infers it from the `GOROOT`? – smwikipedia Nov 27 '16 at 04:52
  • 1
    And the `set` part gives me an impression that it is adding those variables to my environment. – smwikipedia Nov 27 '16 at 04:55
  • I assume the go tool is calling the win32 api to get it's own location then computing a default path from there. See example of that api here: http://stackoverflow.com/questions/2647429/c-windows-path-to-the-folder-where-the-executable-is-located – David Budworth Nov 27 '16 at 16:34
  • as for the "set", `go env` prints the environment as is appropriate to set it on the host platform. ie: on a mac (via homebrew), it prints `GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64"` – David Budworth Nov 27 '16 at 16:38
6

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.

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
  • 1
    I'm assuming you don't literally do: `export GOPATH=/home/user/go/src`, but instead do: `export GOPATH=/home/user/go`, otherwise you'd have $HOME/go/src/src/STUFF YOU GO GET – David Budworth Nov 27 '16 at 16:40
  • Yes, it was a mistake and you're right. The correct path is `export GOPATH=$HOME/go` or `export GOPATH=/user_path/go`. I'll edit my answer. – Chiheb Nexus Nov 27 '16 at 17:16
6

Please type the following command to your command prompt

> go env GOENV
/root/.config/go/env
weaming
  • 5,605
  • 1
  • 23
  • 15
0

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.

Jaimil Patel
  • 1,301
  • 6
  • 13