Ian Lance Taylor | 5cf8071 | 2017-11-14 06:45:22 -0800 | [diff] [blame] | 1 | The GOPATH environment variable specifies the location of your workspace. If no GOPATH is set, it is assumed to be `$HOME/go` on Unix systems and `%USERPROFILE%\go` on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable. This page explains how to set this variable on various platforms. |
| 2 | |
| 3 | |
| 4 | - [Unix systems](#unix-systems) |
| 5 | * [Bash](#bash) |
| 6 | * [Zsh](#zsh) |
| 7 | - [Windows](#windows) |
| 8 | |
| 9 | # Unix systems |
| 10 | |
| 11 | `GOPATH` can be any directory on your system. In Unix examples, we will set it to `$HOME/work`. Note that `GOPATH` must not be the same path as your Go installation. Another common setup is to set GOPATH=$HOME. |
| 12 | |
| 13 | ## Bash |
| 14 | |
| 15 | Edit your `~/.bash_profile` to add the following line: |
| 16 | ```bash |
| 17 | export GOPATH=$HOME/work |
| 18 | ``` |
| 19 | |
| 20 | Save and exit your editor. Then, source your `~/.bash_profile`. |
| 21 | ```bash |
| 22 | source ~/.bash_profile |
| 23 | ``` |
| 24 | |
| 25 | > Note: Set the GOBIN path to generate a binary file when `go install` is run. |
| 26 | > ```bash |
| 27 | > export GOBIN=$HOME/work/bin |
| 28 | > ``` |
| 29 | |
| 30 | ## Zsh |
| 31 | |
| 32 | Edit your `~/.zshrc` file to add the following line: |
| 33 | |
| 34 | ```bash |
| 35 | export GOPATH=$HOME/work |
| 36 | ``` |
| 37 | Save and exit your editor. Then, source your `~/.zshrc`. |
| 38 | ```bash |
| 39 | $ source ~/.zshrc |
| 40 | ``` |
| 41 | |
| 42 | # Windows |
| 43 | |
| 44 | Your workspace can be located wherever you like, |
| 45 | but we'll use `C:\work` in this example. |
| 46 | Note that `GOPATH` must not be the same path as your Go installation. |
| 47 | |
| 48 | * Create folder at `C:\work`. |
| 49 | * Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System". |
| 50 | * From the menu on the left, select the "Advanced systems settings". |
| 51 | * Click the "Environment Variables" button at the bottom. |
| 52 | * Click "New" from the "User variables" section. |
| 53 | * Type `GOPATH` into the "Variable name" field. |
| 54 | * Type `C:\work` into the "Variable value" field. |
| 55 | * Click OK. |