Go Environment Variables: Difference between revisions
(→GOOS) |
(→GOPATH) |
||
Line 5: | Line 5: | ||
=<tt>GOPATH</tt>= | =<tt>GOPATH</tt>= | ||
<FONT COLOR=DARKKHAKI>The <code>GOPATH</code> environment variable defines the [[Go_Language_Modularization#Workspaces|workspace]] directory. Go tools assume that your code is under the path designated by <code>GOPATH</code>. Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font> | <FONT COLOR=DARKKHAKI>The <code>GOPATH</code> environment variable defines the [[Go_Language_Modularization#Workspaces|workspace]] directory. Go tools assume that your code is under the path designated by <code>GOPATH</code>. Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]]. Also see {{Internal|Go_Language_Modularization#Relationship_between_Workspace_and_GOPATH|Relationship between Workspace and <tt>GOPATH</tt>}}</font> | ||
To display the value, as seen by the Go runtime, execute: | To display the value, as seen by the Go runtime, execute: | ||
Line 54: | Line 54: | ||
</pre> | </pre> | ||
</font> | </font> | ||
=<tt>GOROOT</tt>= | =<tt>GOROOT</tt>= | ||
<font color=darkkhaki>Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font> | <font color=darkkhaki>Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font> |
Revision as of 01:34, 8 September 2023
External
Internal
GOPATH
The GOPATH
environment variable defines the workspace directory. Go tools assume that your code is under the path designated by GOPATH
. Used to search for packages during compilation, when packages are being imported. Also see
To display the value, as seen by the Go runtime, execute:
go env GOPATH
For documentation on GOPATH
, execute:
go help gopath
As of Go 1.16, the module-aware mode is turned on by default and the value of GOPATH
is ignored while resolving package dependencies. In module-aware mode, GOPATH
no longer defines the meaning of imports during a build, but it still stores downloaded dependencies in ${GOPATH}/pkg/mod
and installed commands in ${GOPATH}/bin
, unless GOBIN
is set.
For more details see:
GOPATH is used by the compiler to locate source files and package objects for the packages listed by the import statements in source files being compiled.
It must contain absolute paths.
GOPATH content lists directories on the local file system. GOPATH may contain multiple directories. On UNIX, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. GOPATH must be set every time we need to get and build packages outside the standard Go tree.
The compiler loops over the values listed in GOPATH, in order, appends /src/<package-path-string-literal> to them, as declared in the import statement, and looks into the directories whose name are thus generated for source files that belong to the required packages. This is why it is important that the package and the directory containing the package sources have the same name: if they don't, the compilation fails to find the source file.
Each directory listed in GOPATH must have a prescribed structure:
- The src directory holds source code. The path below src determines the import path or executable name.
- The pkg directory holds installed package objects. As in the Go tree, each target operating system and architecture pair has its own subdirectory of pkg: pkg/GOOS_GOARCH.
If DIR is a directory listed in the GOPATH, a package with source in DIR/src/x/y can be imported as "x/y" and has its compiled form installed to "DIR/pkg/GOOS_GOARCH/x/y.a".
- The bin directory holds compiled commands. Each command is named for its source directory, but only the final element, not the entire path. That is, the command with source in DIR/src/x/blah is installed into DIR/bin/blah, not DIR/bin/x/blah. The "x/" prefix is stripped so that you can add DIR/bin to your PATH to get at the installed commands.
Relative Order
Order in which root directories are listed in GOPATH matters. If two directories contain packages with the same name, the compiler will stop searching once it finds the first package that satisfies the import statement. The compiler first look into Go installation directory, then into the directories listed in GOPATH. The behavior corresponding to the package whose root directory exists in the Go installation, and then is listed first in the GOPATH list is used.
"internal" directories
Code in or below a directory named "internal" is importable only by code in the directory tree rooted at the parent of "internal". For more details see https://golang.org/s/go14internal.
Vendor directories
In-line Documentation
go help gopath
GOROOT
Used to search for packages during compilation, when packages are being imported. To display the value, as seen by the Go runtime, execute:
go env GOROOT
The Go runtime installation directory. The compiler looks for packages that are imported at location referenced by the GOROOT and GOPATH variables.
GOROOT must be set at installation, Go runtime expects to find the variable in the environment, unless the runtime is installed in /usr/local/go.
GO111MODULE
Controls whether the go
tool runs in module-aware mode or GOPATH mode. Accepts the following values:
- "off": run in GOPATH mode and ignore
go.mod
. - "on": run. in module-aware mode, even if no
go.mod
file is present. - "auto": run in module-aware mode if a
go.mod
file is present.
For more details see:
GOBIN
If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin. GOBIN must be an absolute path.
GOOS
"darwin" for MacOS.
GOARCH
"amd64"