Skip to content

Commit 74628a8

Browse files
committed
doc, cmd/go: adjust documentation for default GOPATH
Replaces CL 33356. Fixes #17262. Change-Id: Idfb2343e90771775e51a66c63760f458737a288c Reviewed-on: https://go-review.googlesource.com/33730 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5d1b53a commit 74628a8

File tree

7 files changed

+87
-98
lines changed

7 files changed

+87
-98
lines changed

doc/articles/go_command.html

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ <h2>Go's conventions</h2>
9797
deduce where to obtain the source code.</p>
9898

9999
<p>Second, the place to store sources in the local file system is derived
100-
in a known way from the import path. Specifically, the first choice
101-
is <code>$GOPATH/src/&lt;import-path&gt;</code>. If <code>$GOPATH</code> is
102-
unset, the go command will fall back to storing source code alongside the
103-
standard Go packages, in <code>$GOROOT/src/&lt;import-path&gt;</code>.
100+
in a known way from the import path, specifically
101+
<code>$GOPATH/src/&lt;import-path&gt;</code>.
102+
If unset, <code>$GOPATH</code> defaults to a subdirectory
103+
named <code>go</code> in the user's home directory.
104104
If <code>$GOPATH</code> is set to a list of paths, the go command tries
105105
<code>&lt;dir&gt;/src/&lt;import-path&gt;</code> for each of the directories in
106-
that list.</p>
106+
that list.
107+
</p>
107108

108109
<p>Each of those trees contains, by convention, a top-level directory named
109110
"<code>bin</code>", for holding compiled executables, and a top-level directory
@@ -137,28 +138,13 @@ <h2>Go's conventions</h2>
137138

138139
<h2>Getting started with the go command</h2>
139140

140-
<p>Finally, a quick tour of how to use the go command, to supplement
141-
the information in <a href="/doc/code.html">How to Write Go Code</a>,
142-
which you might want to read first. Assuming you want
143-
to keep your source code separate from the Go distribution source
144-
tree, the first step is to set <code>$GOPATH</code>, the one piece of global
145-
configuration that the go command needs. The <code>$GOPATH</code> can be a
146-
list of directories, but by far the most common usage should be to set it to a
147-
single directory. In particular, you do not need a separate entry in
148-
<code>$GOPATH</code> for each of your projects. One <code>$GOPATH</code> can
149-
support many projects.</p>
150-
151-
<p>Here’s an example. Let’s say we decide to keep our Go code in the directory
152-
<code>$HOME/mygo</code>. We need to create that directory and set
153-
<code>$GOPATH</code> accordingly.</p>
141+
<p>Finally, a quick tour of how to use the go command.
142+
As mentioned above, the default <code>$GOPATH</code> on Unix is <code>$HOME/go</code>.
143+
We'll store our programs there.
144+
To use a different location, you can set <code>$GOPATH</code>;
145+
see <a href="/doc/code.html">How to Write Go Code</a> for details.
154146

155-
<pre>
156-
$ mkdir $HOME/mygo
157-
$ export GOPATH=$HOME/mygo
158-
$
159-
</pre>
160-
161-
<p>Into this directory, we now add some source code. Suppose we want to use
147+
<p>We first add some source code. Suppose we want to use
162148
the indexing library from the codesearch project along with a left-leaning
163149
red-black tree. We can install both with the "<code>go get</code>"
164150
subcommand:</p>
@@ -169,8 +155,8 @@ <h2>Getting started with the go command</h2>
169155
$
170156
</pre>
171157

172-
<p>Both of these projects are now downloaded and installed into our
173-
<code>$GOPATH</code> directory. The one tree now contains the two directories
158+
<p>Both of these projects are now downloaded and installed into <code>$HOME/go</code>,
159+
which contains the two directories
174160
<code>src/github.com/google/codesearch/index/</code> and
175161
<code>src/github.com/petar/GoLLRB/llrb/</code>, along with the compiled
176162
packages (in <code>pkg/</code>) for those libraries and their dependencies.</p>
@@ -184,6 +170,7 @@ <h2>Getting started with the go command</h2>
184170
("<code>...</code>"):</p>
185171

186172
<pre>
173+
$ cd $HOME/go/src
187174
$ go list ./...
188175
github.com/google/codesearch/cmd/cgrep
189176
github.com/google/codesearch/cmd/cindex
@@ -215,7 +202,7 @@ <h2>Getting started with the go command</h2>
215202
current directory:</p>
216203

217204
<pre>
218-
$ cd $GOPATH/src/github.com/google/codesearch/regexp
205+
$ cd github.com/google/codesearch/regexp
219206
$ go list
220207
github.com/google/codesearch/regexp
221208
$ go test -v
@@ -244,9 +231,6 @@ <h2>Getting started with the go command</h2>
244231
and complexity in the tool. Typing an extra directory name or two is a small
245232
price to pay for the increased simplicity and power.</p>
246233

247-
<p>As the example shows, it’s fine to work with packages from many different
248-
projects at once within a single <code>$GOPATH</code> root directory.</p>
249-
250234
<h2>Limitations</h2>
251235

252236
<p>As mentioned above, the go command is not a general-purpose build

doc/code.html

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,43 @@ <h3 id="GOPATH">The <code>GOPATH</code> environment variable</h3>
120120

121121
<p>
122122
The <code>GOPATH</code> environment variable specifies the location of your
123-
workspace. It is likely the only environment variable you'll need to set
124-
when developing Go code.
123+
workspace. It defaults to a directory named <code>go</code> inside your home directory,
124+
so <code>$HOME/go</code> on Unix,
125+
<code>$home/go</code> on Plan 9,
126+
and <code>%USERPROFILE%\go</code> (usually <code>C:\Users\YourName\go</code>) on Windows.
127+
If you would like to work in a different location, you will need to set
128+
<code>GOPATH</code> to the path to that directory.
129+
(Another common setup is to set <code>GOPATH=$HOME</code>.)
130+
Note that <code>GOPATH</code> must <b>not</b> be the
131+
same path as your Go installation.
125132
</p>
126133

127134
<p>
128-
To get started, create a workspace directory and set <code>GOPATH</code>
129-
accordingly. Your workspace can be located wherever you like, but we'll use
130-
<code>$HOME/work</code> in this document. Note that this must <b>not</b> be the
131-
same path as your Go installation.
132-
(Another common setup is to set <code>GOPATH=$HOME</code>.)
135+
The command <code>go</code> <code>env</code> <code>GOPATH</code>
136+
prints the effective current <code>GOPATH</code>;
137+
it prints the default location if the environment variable is unset.
138+
</p>
139+
140+
<p>
141+
For convenience, add the workspace's <code>bin</code> subdirectory
142+
to your <code>PATH</code>:
133143
</p>
134144

135145
<pre>
136-
$ <b>mkdir $HOME/work</b>
137-
$ <b>export GOPATH=$HOME/work</b>
146+
$ <b>export PATH=$PATH:$(go env GOPATH)/bin</b>
138147
</pre>
139148

140149
<p>
141-
For convenience, add the workspace's <code>bin</code> subdirectory
142-
to your <code>PATH</code>:
150+
The scripts in the rest of this document use <code>$GOPATH</code>
151+
instead of <code>$(go env GOPATH)</code> for brevity.
152+
To make the scripts run as written
153+
if you have not set GOPATH,
154+
you can substitute $HOME/go in those commands
155+
or else run:
143156
</p>
144157

145158
<pre>
146-
$ <b>export PATH=$PATH:$GOPATH/bin</b>
159+
$ <b>export GOPATH=$(go env GOPATH)</b>
147160
</pre>
148161

149162
<p>

doc/go_faq.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ <h3 id="git_https">
10941094
<ul>
10951095
<li>Manually clone the repository in the expected package directory:
10961096
<pre>
1097-
$ cd $GOPATH/src/github.com/username
1097+
$ cd src/github.com/username
10981098
$ git clone [email protected]:username/package.git
10991099
</pre>
11001100
</li>

doc/install-source.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ <h2 id="environment">Optional environment variables</h2>
430430
<ul>
431431
<li><code>$GOROOT</code>
432432
<p>
433-
The root of the Go tree, often <code>$HOME/go</code>.
433+
The root of the Go tree, often <code>$HOME/go1.X</code>.
434434
Its value is built into the tree when it is compiled, and
435435
defaults to the parent of the directory where <code>all.bash</code> was run.
436436
There is no need to set this unless you want to switch between multiple
@@ -632,7 +632,7 @@ <h2 id="environment">Optional environment variables</h2>
632632
</p>
633633

634634
<pre>
635-
export GOROOT=$HOME/go
635+
export GOROOT=$HOME/go1.X
636636
export GOARCH=amd64
637637
export GOOS=linux
638638
</pre>

doc/install.html

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ <h4 id="tarball_non_standard">Installing to a custom location</h4>
117117
</p>
118118

119119
<p>
120-
For example, if you installed Go to your home directory you should add the
121-
following commands to <code>$HOME/.profile</code>:
120+
For example, if you installed Go to your home directory you should add
121+
commands like the following to <code>$HOME/.profile</code>:
122122
</p>
123123

124124
<pre>
125-
export GOROOT=$HOME/go
125+
export GOROOT=$HOME/go1.X
126126
export PATH=$PATH:$GOROOT/bin
127127
</pre>
128128

@@ -219,37 +219,16 @@ <h2 id="testing">Test your installation</h2>
219219
</p>
220220

221221
<p>
222-
Create a directory to contain your <a href="code.html#Workspaces">workspace</a>,
223-
<code class="testUnix">$HOME/work</code>
224-
<code class="testWindows" style="display: none">C:\work</code>
225-
for example, and set the <code>GOPATH</code> environment
226-
variable to point to that location.
222+
Create your <a href="code.html#Workspaces">workspace</a> directory,
223+
<code class="testUnix">$HOME/go</code><code class="testWindows">%USERPROFILE%\go</code>.
224+
(If you'd like to use a different directory,
225+
you will need to set the <code>GOPATH</code> environment variable;
226+
see <a href="code.html#Workspaces">How to Write Go Code</a> for details.)
227227
</p>
228228

229-
<pre class="testUnix">
230-
$ <b>export GOPATH=$HOME/work</b>
231-
</pre>
232-
233-
<pre class="testWindows" style="display: none">
234-
C:\&gt; <b>set GOPATH=C:\work</b>
235-
</pre>
236-
237229
<p>
238-
<span class="testUnix">
239-
You should put the above command in your shell startup script
240-
(<code>$HOME/.profile</code> for example).
241-
</span>
242-
<span class="testWindows">
243-
On Windows, follow the <a href="#windows_env">instructions above</a> to set the
244-
<code>GOPATH</code> environment variable on your system.
245-
</span>
246-
</p>
247-
248-
<p>
249-
Next, make the directories <code>src/github.com/user/hello</code> inside your
250-
workspace (if you use GitHub, substitute your user name for <code>user</code>),
251-
and inside the <code>hello</code> directory create a file named <code>hello.go</code>
252-
with the following contents:
230+
Next, make the directory <code>src/hello</code> inside your workspace,
231+
and in that directory create a file named <code>hello.go</code> that looks like:
253232
</p>
254233

255234
<pre>
@@ -263,37 +242,46 @@ <h2 id="testing">Test your installation</h2>
263242
</pre>
264243

265244
<p>
266-
Then compile it with the <code>go</code> tool:
245+
Then build it with the <code>go</code> tool:
267246
</p>
268247

269248
<pre class="testUnix">
270-
$ <b>go install github.com/user/hello</b>
249+
$ <b>cd $HOME/go/src/hello
250+
$ <b>go build</b>
271251
</pre>
272252

273253
<pre class="testWindows" style="display: none">
274-
C:\&gt; <b>go install github.com/user/hello</b>
254+
C:\&gt; <b>cd %USERPROFILE%\go\src\hello<b>
255+
C:\Users\Gopher\go\src\hello&gt; <b>go build</b>
275256
</pre>
276257

277258
<p>
278-
The command above will put an executable command named <code>hello</code>
279-
(or <code>hello.exe</code>) inside the <code>bin</code> directory of your workspace.
280-
Execute the command to see the greeting:
259+
The command above will build an executable named
260+
<code class="testUnix">hello</code><code class="testWindows">hello.exe</code>
261+
in the directory alongside your source code.
262+
Execute it to see the greeting:
281263
</p>
282264

283265
<pre class="testUnix">
284-
$ <b>$GOPATH/bin/hello</b>
266+
$ <b>./hello</b>
285267
hello, world
286268
</pre>
287269

288270
<pre class="testWindows" style="display: none">
289-
C:\&gt; <b>%GOPATH%\bin\hello</b>
271+
C:\Users\Gopher\go\src\hello&gt; <b>hello</b>
290272
hello, world
291273
</pre>
292274

293275
<p>
294276
If you see the "hello, world" message then your Go installation is working.
295277
</p>
296278

279+
<p>
280+
You can run <code>go</code> <code>install</code> to install the binary into
281+
your workspace's <code>bin</code> directory
282+
or <code>go</code> <code>clean</code> to remove it.
283+
</p>
284+
297285
<p>
298286
Before rushing off to write Go code please read the
299287
<a href="/doc/code.html">How to Write Go Code</a> document,

src/cmd/go/alldocs.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,10 @@
929929
// On Windows, the value is a semicolon-separated string.
930930
// On Plan 9, the value is a list.
931931
//
932-
// GOPATH must be set to get, build and install packages outside the
933-
// standard Go tree.
932+
// If the environment variable is unset, GOPATH defaults
933+
// to a subdirectory named "go" in the user's home directory
934+
// ($HOME/go on Unix, %USERPROFILE%\go on Windows).
935+
// Run "go env GOPATH" to see the current GOPATH.
934936
//
935937
// Each directory listed in GOPATH must have a prescribed structure:
936938
//
@@ -958,9 +960,9 @@
958960
//
959961
// Here's an example directory layout:
960962
//
961-
// GOPATH=/home/user/gocode
963+
// GOPATH=/home/user/go
962964
//
963-
// /home/user/gocode/
965+
// /home/user/go/
964966
// src/
965967
// foo/
966968
// bar/ (go code in package bar)
@@ -986,7 +988,7 @@
986988
// by code in the directory tree rooted at the parent of "internal".
987989
// Here's an extended version of the directory layout above:
988990
//
989-
// /home/user/gocode/
991+
// /home/user/go/
990992
// src/
991993
// crash/
992994
// bang/ (go code in package bang)
@@ -1024,7 +1026,7 @@
10241026
// but with the "internal" directory renamed to "vendor"
10251027
// and a new foo/vendor/crash/bang directory added:
10261028
//
1027-
// /home/user/gocode/
1029+
// /home/user/go/
10281030
// src/
10291031
// crash/
10301032
// bang/ (go code in package bang)

src/cmd/go/help.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ On Unix, the value is a colon-separated string.
289289
On Windows, the value is a semicolon-separated string.
290290
On Plan 9, the value is a list.
291291
292-
GOPATH must be set to get, build and install packages outside the
293-
standard Go tree.
292+
If the environment variable is unset, GOPATH defaults
293+
to a subdirectory named "go" in the user's home directory
294+
($HOME/go on Unix, %USERPROFILE%\go on Windows).
295+
Run "go env GOPATH" to see the current GOPATH.
294296
295297
Each directory listed in GOPATH must have a prescribed structure:
296298
@@ -318,9 +320,9 @@ of DIR/bin. GOBIN must be an absolute path.
318320
319321
Here's an example directory layout:
320322
321-
GOPATH=/home/user/gocode
323+
GOPATH=/home/user/go
322324
323-
/home/user/gocode/
325+
/home/user/go/
324326
src/
325327
foo/
326328
bar/ (go code in package bar)
@@ -346,7 +348,7 @@ Code in or below a directory named "internal" is importable only
346348
by code in the directory tree rooted at the parent of "internal".
347349
Here's an extended version of the directory layout above:
348350
349-
/home/user/gocode/
351+
/home/user/go/
350352
src/
351353
crash/
352354
bang/ (go code in package bang)
@@ -384,7 +386,7 @@ Here's the example from the previous section,
384386
but with the "internal" directory renamed to "vendor"
385387
and a new foo/vendor/crash/bang directory added:
386388
387-
/home/user/gocode/
389+
/home/user/go/
388390
src/
389391
crash/
390392
bang/ (go code in package bang)

0 commit comments

Comments
 (0)