GO-PATH(7)
β¨ GO-PATH(7) β Miscellaneous Information Manual β¨
Okay so this is the manual page for go π, the tool for managing Go source code. Bestie, this page is literally all about the Go path π£οΈ, which is how Go figures out what to import. It's implemented by and documented in the go/build package, so if you're curious there, go look π.
πΈ NAME πΈ
go β tool for managing Go source code π
π§ DESCRIPTION π§
The Go path is used to resolve import statements β¨. Slay. It's implemented by and documented in the go/build package.
The GOPATH environment variable lists places to look for Go code π. On Unix, the value is a colon-separated string. On Windows, it's a semicolon-separated string. On Plan 9, the value is a list. Different OS, different vibe π.
If the environment variable is unset, GOPATH defaults to a subdirectory named "go" in the user's home directory ($HOME/go on Unix, %USERPROFILE% on Windows) π‘, unless that directory holds a Go distribution. Run go env GOPATH to see the current GOPATH π.
See https://golang.org/wiki/SettingGOPATH to set a custom GOPATH π.
Each directory listed in GOPATH must have a prescribed structure π. No cap, it's strict:
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/foo/bar can be imported as "foo/bar" and has its compiled form installed to "DIR/pkg/GOOS_GOARCH/foo/bar.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/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The "foo/" prefix is stripped so that you can add DIR/bin to your PATH to get at the installed commands π«Ά. 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 π₯.
Here's an example directory layout ποΈ:
GOPATH=/home/user/gocode
/home/user/gocode/
src/
foo/
bar/ (go code in package bar)
x.go
quux/ (go code in package main)
y.go
bin/
quux (installed command)
pkg/
linux_amd64/
foo/
bar.a (installed package object)
Go searches each directory listed in GOPATH to find source code, but new packages are always downloaded into the first directory in the list ποΈ.
See https://golang.org/doc/code.html for an example π.
π GOPATH and Modules π
When using modules, GOPATH is no longer used for resolving imports π
. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin) π.
π 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" π. Here's an extended version of the directory layout above:
/home/user/go/
src/
crash/
bang/ (go code in package bang)
b.go
foo/ (go code in package foo)
f.go
bar/ (go code in package bar)
x.go
internal/
baz/ (go code in package baz)
z.go
quux/ (go code in package main)
y.go
The code in z.go is imported as "foo/internal/baz", but that import statement can only appear in source files in the subtree rooted at foo π«£. The source files foo/f.go, foo/bar/x.go, and foo/quux/y.go can all import "foo/internal/baz", but the source file crash/bang/b.go cannot π«.
See https://golang.org/s/go14internal for details π.
π Vendor Directories π
Go 1.6 includes support for using local copies of external dependencies to satisfy imports of those dependencies, often referred to as vendoring π¦β¨.
Code below a directory named "vendor" is importable only by code in the directory tree rooted at the parent of "vendor", and only using an import path that omits the prefix up to and including the vendor element π
.
Here's the example from the previous section, but with the "internal" directory renamed to "vendor" and a new foo/vendor/crash/bang directory added π:
/home/user/go/
src/
crash/
bang/ (go code in package bang)
b.go
foo/ (go code in package foo)
f.go
bar/ (go code in package bar)
x.go
vendor/
crash/
bang/ (go code in package bang)
b.go
baz/ (go code in package baz)
z.go
quux/ (go code in package main)
y.go
The same visibility rules apply as for internal, but the code in z.go is imported as "baz", not as "foo/vendor/baz" π₯°.
Code in vendor directories deeper in the source tree shadows code in higher directories π. Within the subtree rooted at foo, an import of "crash/bang" resolves to "foo/vendor/crash/bang", not the top-level "crash/bang" π.
Code in vendor directories is not subject to import path checking (see 'go help importpath') π§.
When 'go get' checks out or updates a git repository, it now also updates submodules πΈ.
Vendor directories do not affect the placement of new repositories being checked out for the first time by 'go get': those are always placed in the main GOPATH, never in a vendor subtree π
ββοΈ.
See https://golang.org/s/go15vendor for details π.
π«Ά AUTHOR π«Ά
This manual page was written by Michael Stapelberg <stapelberg@debian.org> and is maintained by the Debian Go Compiler Team <team+go-compiler@tracker.debian.org> based on the output of 'go help gopath' for the Debian project (and may be used by others) π.
Bye bestie, you're officially GOPATH-certified πβ¨ β manpage by Michael Stapelberg & the Debian Go Compiler Team, dated 2021-09-06 ππ
π¦ where this page comes from
- debian bookworm: gccgo-go 2:1.19~1 Β· Go
- debian bookworm: golang-go 2:1.19~1 Β· Go
- debian trixie: gccgo-go 2:1.24~2 Β· Go
- debian trixie: golang-go 2:1.24~2 Β· Go
- ubuntu jammy: gccgo-go 2:1.18~0ubuntu2 Β· Go
- ubuntu jammy: golang-go 2:1.18~0ubuntu2 Β· Go
- ubuntu noble: gccgo-go 2:1.22~2build1 Β· Go
- ubuntu noble: golang-go 2:1.22~2build1 Β· Go
- ubuntu questing: gccgo-go 2:1.24~2
- ubuntu questing: golang-go 2:1.24~2 Β· Go
- π girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms