TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 1 | # Setting `GOPATH` |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | |
| 6 | - [Unix systems](#unix-systems) |
| 7 | * [Bash](#bash) |
| 8 | * [Zsh](#zsh) |
| 9 | * [fish](#fish) |
| 10 | - [Windows](#windows) |
| 11 | |
| 12 | # Unix systems |
| 13 | |
Filippo Valsorda | 4056ea9 | 2018-08-13 01:17:19 -0400 | [diff] [blame] | 14 | `GOPATH` can be any directory on your system. In Unix examples, we will set it to `$HOME/go` (the default since Go 1.8). Note that `GOPATH` must not be the same path as your Go installation. Another common setup is to set `GOPATH=$HOME`. |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 15 | |
| 16 | ## Bash |
| 17 | |
| 18 | Edit your `~/.bash_profile` to add the following line: |
| 19 | ```bash |
| 20 | export GOPATH=$HOME/go |
| 21 | ``` |
| 22 | |
| 23 | Save and exit your editor. Then, source your `~/.bash_profile`. |
| 24 | ```bash |
| 25 | source ~/.bash_profile |
| 26 | ``` |
| 27 | |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 28 | ## Zsh |
| 29 | |
| 30 | Edit your `~/.zshrc` file to add the following line: |
| 31 | |
| 32 | ```bash |
| 33 | export GOPATH=$HOME/go |
| 34 | ``` |
| 35 | Save and exit your editor. Then, source your `~/.zshrc`. |
| 36 | ```bash |
Filippo Valsorda | 4056ea9 | 2018-08-13 01:17:19 -0400 | [diff] [blame] | 37 | source ~/.zshrc |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 38 | ``` |
| 39 | |
| 40 | ## fish |
| 41 | |
| 42 | ```bash |
| 43 | set -x -U GOPATH $HOME/go |
| 44 | ``` |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 45 | |
Filippo Valsorda | 4056ea9 | 2018-08-13 01:17:19 -0400 | [diff] [blame] | 46 | The `-x` is used to specify that this variable should be exported |
| 47 | and the `-U` makes this a universal variable, available to all sessions and |
| 48 | persistent. |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 49 | |
| 50 | # Windows |
| 51 | |
| 52 | Your workspace can be located wherever you like, |
| 53 | but we'll use `C:\go-work` in this example. |
| 54 | |
Filippo Valsorda | 4056ea9 | 2018-08-13 01:17:19 -0400 | [diff] [blame] | 55 | __NOTE:__ `GOPATH` must not be the same path as your Go installation. |
TThomasV | 05f7b46 | 2018-06-03 13:25:33 +0100 | [diff] [blame] | 56 | |
| 57 | * Create folder at `C:\go-work`. |
| 58 | * Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System". |
| 59 | * From the menu on the left, select the "Advanced systems settings". |
| 60 | * Click the "Environment Variables" button at the bottom. |
| 61 | * Click "New" from the "User variables" section. |
| 62 | * Type `GOPATH` into the "Variable name" field. |
| 63 | * Type `C:\go-work` into the "Variable value" field. |
| 64 | * Click OK. |
| 65 | |
| 66 | ## Windows 10 |
Filippo Valsorda | 4056ea9 | 2018-08-13 01:17:19 -0400 | [diff] [blame] | 67 | There is a faster way to edit `Environment Variables` via search: |
| 68 | * Left click on "Search" and type `env` or `environment`. |
| 69 | * Select "Edit environment variables for your account". |
Tristan | b20f126 | 2018-09-24 11:35:37 +0800 | [diff] [blame] | 70 | * ... and follow steps above. |
| 71 | |
| 72 | ## Windows 10 (cli version) |
| 73 | * Open a command prompt (windows-key + r then type "cmd") or a powershell window (windows-key + i) |
Tristan | e89f2b5 | 2018-09-24 11:37:54 +0800 | [diff] [blame] | 74 | * Type `setx GOPATH %USERPROFILE%\go` (this will set the `GOPATH` to your `[home folder]\go` e.g. `C:\Users\yourusername\go` |
Tristan | b20f126 | 2018-09-24 11:35:37 +0800 | [diff] [blame] | 75 | * Close the command or powershell window (the environment variable is only available for new command or powershell windows, not for the current window). |