blob: b0327f81893cba0ac78807cfbb28a90d8d7c3721 [file] [log] [blame] [view]
TThomasV05f7b462018-06-03 13:25:33 +01001# Setting `GOPATH`
2
3The `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 Valsorda4056ea92018-08-13 01:17:19 -040014`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`.
TThomasV05f7b462018-06-03 13:25:33 +010015
16## Bash
17
18Edit your `~/.bash_profile` to add the following line:
19```bash
20export GOPATH=$HOME/go
21```
22
23Save and exit your editor. Then, source your `~/.bash_profile`.
24```bash
25source ~/.bash_profile
26```
27
TThomasV05f7b462018-06-03 13:25:33 +010028## Zsh
29
30Edit your `~/.zshrc` file to add the following line:
31
32```bash
33export GOPATH=$HOME/go
34```
35Save and exit your editor. Then, source your `~/.zshrc`.
36```bash
Filippo Valsorda4056ea92018-08-13 01:17:19 -040037source ~/.zshrc
TThomasV05f7b462018-06-03 13:25:33 +010038```
39
40## fish
41
42```bash
43set -x -U GOPATH $HOME/go
44```
TThomasV05f7b462018-06-03 13:25:33 +010045
Filippo Valsorda4056ea92018-08-13 01:17:19 -040046The `-x` is used to specify that this variable should be exported
47and the `-U` makes this a universal variable, available to all sessions and
48persistent.
TThomasV05f7b462018-06-03 13:25:33 +010049
50# Windows
51
52Your workspace can be located wherever you like,
53but we'll use `C:\go-work` in this example.
54
Filippo Valsorda4056ea92018-08-13 01:17:19 -040055__NOTE:__ `GOPATH` must not be the same path as your Go installation.
TThomasV05f7b462018-06-03 13:25:33 +010056
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 Valsorda4056ea92018-08-13 01:17:19 -040067There 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".
Tristanb20f1262018-09-24 11:35:37 +080070* ... 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)
Tristane89f2b52018-09-24 11:37:54 +080074* Type `setx GOPATH %USERPROFILE%\go` (this will set the `GOPATH` to your `[home folder]\go` e.g. `C:\Users\yourusername\go`
Tristanb20f1262018-09-24 11:35:37 +080075* Close the command or powershell window (the environment variable is only available for new command or powershell windows, not for the current window).