blob: 2db5e8c7746487d93c79b8025e0d40a8373b92f4 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
2
3The GOPATH environment variable is used to specify directories outside of $GOROOT that contain the source for Go projects and their binaries.
4
5See the [go command](http://golang.org/cmd/go/#hdr-GOPATH_environment_variable) and [go/build package](http://golang.org/pkg/go/build/) documentation for more details.
6
7Since the $GOPATH variable can be a list, the rest of this document will use $GOPATH to mean the first element unless otherwise specified.
8
9## Integrating GOPATH
10
jethrogbb466cef2015-06-09 14:13:38 -070011On OS X or Linux (bash), adding the following expression to PATH will add all $GOPATH/bin directories.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110012```
13${GOPATH//://bin:}/bin
14```
15
16## Directory layout
17
18The source for a package with the import path ` "X/Y/Z" ` is in the directory
19```
20$GOPATH/src/X/Y/Z
21```
22
23The binary for a package with the import path ` "X/Y/Z" ` is in
24```
25$GOPATH/pkg/$GOOS_$GOARCH/X/Y/Z.a
26```
27
28The binary for a command whose source is in ` $GOPATH/src/A/B ` is
29```
30$GOPATH/bin/B
31```
32
jcabmoraa44f6772017-10-19 11:51:32 -070033## Repository integration and creating "go gettable" projects
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110034When fetching a package the go tool looks at the package's import path to discover a URL. For instance, if you attempt to
35```
Oleg Butuzovd14b0232017-05-28 23:25:24 +030036go get github.com/go-kit/kit
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110037```
Oleg Butuzovd14b0232017-05-28 23:25:24 +030038the go tool will get the source from the project hosted at https://github.com/go-kit/kit/. It will clone the repository to
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110039```
Oleg Butuzovd14b0232017-05-28 23:25:24 +030040$GOPATH/src/github.com/go-kit/kit
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110041```
42
Oleg Butuzovd14b0232017-05-28 23:25:24 +030043As a result, if (from your repository project) you import a package that is in the same repository, you need to use its "full" import path - the place "go get" puts it. In this example, if something else wants to import the "kit" package, it should import "github.com/go-kit/kit" rather than "kit".
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110044
45## Tips and tricks
46
Nate Finche4c2acc2015-11-12 22:11:53 -050047### Use a single GOPATH
48
Andrew Prock608c3d12016-10-06 20:34:40 -070049Even though the GOPATH may be a list of directories, it is generally sufficient to use a single GOPATH for all Go code on your machine. Since all packages retrieved with "go get" have a unique URL (and thus a unique path on disk), having more than one GOPATH is almost never necessary when building with the Go tool.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110050
Constantine A. Murenina759bcc2019-01-16 13:27:54 -060051### `vendor/` directories ignored outside of `GOPATH`
Constantine A. Murenin25e6f4a2019-01-16 13:20:19 -060052
Constantine A. Murenina759bcc2019-01-16 13:27:54 -060053Note that as of go 1.11, the `vendor` directories are only supported when `$PWD` is under `$GOPATH`; see https://github.com/golang/go/issues/29670 and https://stackoverflow.com/questions/54140415/go-1-11-ignores-the-vendor-directory-the-errors-give-the-impression-the-direc/54156547#54156547.
Constantine A. Murenin25e6f4a2019-01-16 13:20:19 -060054
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110055## FAQ
56### Why won't ` $GOPATH/src/cmd/mycmd/*.go ` build?
57When the go command is looking for packages, it always looks in ` $GOROOT ` first. This includes directories, so if it finds (as in the case above) a ` cmd/ ` directory in ` $GOROOT ` it won't proceed to look in any of the GOPATH directories. This prevents you from defining your own ` math/matrix ` package as well as your own ` cmd/mycmd ` commands.