Skip to content

Commit 4c4fb01

Browse files
committed
_content/doc: document 'go install pkg@version'
For golang/go#33637 For golang/go#40276 Change-Id: I25ef2024867194bd7dc2e70157fef9123498f49d Reviewed-on: https://go-review.googlesource.com/c/website/+/285452 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent dd5775c commit 4c4fb01

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

_content/doc/mod.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,9 @@ which can be set to `on`, `off`, or `auto`.
948948
`go.mod` file: see [Module commands outside a module](#commands-outside).
949949
* If `GO111MODULE=auto`, the `go` command runs in module-aware mode if a
950950
`go.mod` file is present in the current directory or any parent directory.
951-
In Go 1.15 and lower, this was the default behavior.
951+
In Go 1.15 and lower, this was the default behavior. `go mod` subcommands
952+
and `go install` with a [version query](#version-queries) run in module-aware
953+
mode even if no `go.mod` file is present.
952954

953955
In module-aware mode, `GOPATH` no longer defines the meaning of imports during a
954956
build, but it still stores downloaded dependencies (in `GOPATH/pkg/mod`; see
@@ -1058,9 +1060,6 @@ go get [-d] [-t] [-u] [build flags] [packages]
10581060
Examples:
10591061

10601062
```
1061-
# Install the latest version of a tool.
1062-
$ go get golang.org/x/tools/cmd/goimports
1063-
10641063
# Upgrade a specific module.
10651064
$ go get -d golang.org/x/net
10661065
@@ -1164,6 +1163,80 @@ the `GOBIN` environment variable, which defaults to `$GOPATH/bin` or
11641163
variable](#environment-variables) provides more fine-grained control and
11651164
should be used instead.
11661165

1166+
Since Go 1.16, [`go install`](#go-install) is the recommended command for
1167+
building and installing programs. When used with a version suffix (like
1168+
`@latest` or `@v1.4.6`), `go install` builds packages in module-aware mode,
1169+
ignoring the `go.mod` file in the current directory or any parent directory,
1170+
if there is one.
1171+
1172+
`go get` is more focused on managing requirements in `go.mod`. The `-d` flag
1173+
is deprecated, and in a future release, it will always be enabled.
1174+
1175+
### `go install` {#go-install}
1176+
1177+
Usage:
1178+
1179+
```
1180+
go install [build flags] [packages]
1181+
```
1182+
1183+
Examples:
1184+
1185+
```
1186+
# Install the latest version of a program,
1187+
# ignoring go.mod in the current directory (if any).
1188+
$ go install golang.org/x/tools/gopls@latest
1189+
1190+
# Install a specific version of a program.
1191+
$ go install golang.org/x/tools/[email protected]
1192+
1193+
# Install a program at the version selected by the module in the current directory.
1194+
$ go install golang.org/x/tools/gopls
1195+
1196+
# Install all programs in a directory.
1197+
$ go install ./cmd/...
1198+
```
1199+
1200+
The `go install` command builds and installs the packages named by the paths
1201+
on the command line. Executables (`main` packages) are installed to the
1202+
directory named by the `GOBIN` environment variable, which defaults to
1203+
`$GOPATH/bin` or `$HOME/go/bin` if the `GOPATH` environment variable is not set.
1204+
Executables in `$GOROOT` are installed in `$GOROOT/bin` or `$GOTOOLDIR` instead
1205+
of `$GOBIN`.
1206+
1207+
Since Go 1.16, if the arguments have version suffixes (like `@latest` or
1208+
`@v1.0.0`), `go install` builds packages in module-aware mode, ignoring the
1209+
`go.mod` file in the current directory or any parent directory if there is
1210+
one. This is useful for installing executables without affecting the
1211+
dependencies of the main module.
1212+
1213+
To eliminate ambiguity about which module versions are used in the build, the
1214+
arguments must satisfy the following constraints:
1215+
1216+
* Arguments must be package paths or package patterns (with "`...`" wildcards).
1217+
They must not be standard packages (like `fmt`), meta-patterns (`std`, `cmd`,
1218+
`all`), or relative or absolute file paths.
1219+
* All arguments must have the same version suffix. Different queries are not
1220+
allowed, even if they refer to the same version.
1221+
* All arguments must refer to packages in the same module at the same version.
1222+
* No module is considered the [main module](#glos-main-module). If the module
1223+
containing packages named on the command line has a `go.mod` file, it must not
1224+
contain directives (`replace` and `exclude`) that would cause it to be
1225+
interpreted differently than if it were the main module. The module must not
1226+
require a higher version of itself.
1227+
* Package path arguments must refer to `main` packages. Pattern arguments
1228+
will only match `main` packages.
1229+
1230+
See [Version queries](#version-queries) for supported version query syntax.
1231+
Go 1.15 and lower did not support using version queries with `go install`.
1232+
1233+
If the arguments don't have version suffixes, `go install` may run in
1234+
module-aware mode or `GOPATH` mode, depending on the `GO111MODULE` environment
1235+
variable and the presence of a `go.mod` file. See [Module-aware
1236+
commands](#mod-commands) for details. If module-aware mode is enabled, `go
1237+
install` runs in the context of the main module, which may be different from the
1238+
module containing the package being installed.
1239+
11671240
### `go list -m` {#go-list-m}
11681241

11691242
Usage:

0 commit comments

Comments
 (0)