Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Introduction |
| 2 | |
| 3 | The GOPATH environment variable is used to specify directories outside of $GOROOT that contain the source for Go projects and their binaries. |
| 4 | |
| 5 | See 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 | |
| 7 | Since 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 | |
jethrogb | b466cef | 2015-06-09 14:13:38 -0700 | [diff] [blame] | 11 | On OS X or Linux (bash), adding the following expression to PATH will add all $GOPATH/bin directories. |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 12 | ``` |
| 13 | ${GOPATH//://bin:}/bin |
| 14 | ``` |
| 15 | |
| 16 | ## Directory layout |
| 17 | |
| 18 | The source for a package with the import path ` "X/Y/Z" ` is in the directory |
| 19 | ``` |
| 20 | $GOPATH/src/X/Y/Z |
| 21 | ``` |
| 22 | |
| 23 | The 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 | |
| 28 | The binary for a command whose source is in ` $GOPATH/src/A/B ` is |
| 29 | ``` |
| 30 | $GOPATH/bin/B |
| 31 | ``` |
| 32 | |
jcabmora | a44f677 | 2017-10-19 11:51:32 -0700 | [diff] [blame] | 33 | ## Repository integration and creating "go gettable" projects |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 34 | When 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 Butuzov | d14b023 | 2017-05-28 23:25:24 +0300 | [diff] [blame] | 36 | go get github.com/go-kit/kit |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 37 | ``` |
Oleg Butuzov | d14b023 | 2017-05-28 23:25:24 +0300 | [diff] [blame] | 38 | the go tool will get the source from the project hosted at https://github.com/go-kit/kit/. It will clone the repository to |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 39 | ``` |
Oleg Butuzov | d14b023 | 2017-05-28 23:25:24 +0300 | [diff] [blame] | 40 | $GOPATH/src/github.com/go-kit/kit |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 41 | ``` |
| 42 | |
Oleg Butuzov | d14b023 | 2017-05-28 23:25:24 +0300 | [diff] [blame] | 43 | As 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 Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 44 | |
| 45 | ## Tips and tricks |
| 46 | |
Nate Finch | e4c2acc | 2015-11-12 22:11:53 -0500 | [diff] [blame] | 47 | ### Use a single GOPATH |
| 48 | |
Andrew Prock | 608c3d1 | 2016-10-06 20:34:40 -0700 | [diff] [blame] | 49 | Even 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 Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 50 | |
Constantine A. Murenin | a759bcc | 2019-01-16 13:27:54 -0600 | [diff] [blame] | 51 | ### `vendor/` directories ignored outside of `GOPATH` |
Constantine A. Murenin | 25e6f4a | 2019-01-16 13:20:19 -0600 | [diff] [blame] | 52 | |
Constantine A. Murenin | a759bcc | 2019-01-16 13:27:54 -0600 | [diff] [blame] | 53 | Note 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. Murenin | 25e6f4a | 2019-01-16 13:20:19 -0600 | [diff] [blame] | 54 | |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 55 | ## FAQ |
| 56 | ### Why won't ` $GOPATH/src/cmd/mycmd/*.go ` build? |
| 57 | When 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. |