@@ -124,72 +124,63 @@ and the build list. For example:
124
124
125
125
Maintaining module requirements
126
126
127
- The go.mod file is meant to be readable and editable by both
128
- programmers and tools. The go command itself automatically updates the go.mod file
129
- to maintain a standard formatting and the accuracy of require statements.
130
-
131
- Any go command that finds an unfamiliar import will look up the module
132
- containing that import and add the latest version of that module
133
- to go.mod automatically. In most cases, therefore, it suffices to
134
- add an import to source code and run 'go build', 'go test', or even 'go list':
135
- as part of analyzing the package, the go command will discover
136
- and resolve the import and update the go.mod file.
137
-
138
- Any go command can determine that a module requirement is
139
- missing and must be added, even when considering only a single
140
- package from the module. On the other hand, determining that a module requirement
141
- is no longer necessary and can be deleted requires a full view of
142
- all packages in the module, across all possible build configurations
143
- (architectures, operating systems, build tags, and so on).
144
- The 'go mod tidy' command builds that view and then
145
- adds any missing module requirements and removes unnecessary ones.
127
+ The go.mod file is meant to be readable and editable by both programmers and
128
+ tools. Most updates to dependencies can be performed using "go get" and
129
+ "go mod tidy". Other module-aware build commands may be invoked using the
130
+ -mod=mod flag to automatically add missing requirements and fix inconsistencies.
131
+
132
+ The "go get" command updates go.mod to change the module versions used in a
133
+ build. An upgrade of one module may imply upgrading others, and similarly a
134
+ downgrade of one module may imply downgrading others. The "go get" command
135
+ makes these implied changes as well. See "go help module-get".
136
+
137
+ The "go mod" command provides other functionality for use in maintaining
138
+ and understanding modules and go.mod files. See "go help mod", particularly
139
+ "go help mod tidy" and "go help mod edit".
146
140
147
141
As part of maintaining the require statements in go.mod, the go command
148
142
tracks which ones provide packages imported directly by the current module
149
143
and which ones provide packages only used indirectly by other module
150
144
dependencies. Requirements needed only for indirect uses are marked with a
151
- "// indirect" comment in the go.mod file. Indirect requirements are
145
+ "// indirect" comment in the go.mod file. Indirect requirements may be
152
146
automatically removed from the go.mod file once they are implied by other
153
147
direct requirements. Indirect requirements only arise when using modules
154
148
that fail to state some of their own dependencies or when explicitly
155
149
upgrading a module's dependencies ahead of its own stated requirements.
156
150
157
- Because of this automatic maintenance, the information in go.mod is an
158
- up-to-date, readable description of the build.
159
-
160
- The 'go get' command updates go.mod to change the module versions used in a
161
- build. An upgrade of one module may imply upgrading others, and similarly a
162
- downgrade of one module may imply downgrading others. The 'go get' command
163
- makes these implied changes as well. If go.mod is edited directly, commands
164
- like 'go build' or 'go list' will assume that an upgrade is intended and
165
- automatically make any implied upgrades and update go.mod to reflect them.
166
-
167
- The 'go mod' command provides other functionality for use in maintaining
168
- and understanding modules and go.mod files. See 'go help mod'.
169
-
170
- The -mod build flag provides additional control over updating and use of go.mod.
171
-
172
- If invoked with -mod=readonly, the go command is disallowed from the implicit
173
- automatic updating of go.mod described above. Instead, it fails when any changes
174
- to go.mod are needed. This setting is most useful to check that go.mod does
175
- not need updates, such as in a continuous integration and testing system.
176
- The "go get" command remains permitted to update go.mod even with -mod=readonly,
177
- and the "go mod" commands do not take the -mod flag (or any other build flags).
151
+ The -mod build flag provides additional control over the updating and use of
152
+ go.mod for commands that build packages like "go build" and "go test".
153
+
154
+ If invoked with -mod=readonly (the default in most situations), the go command
155
+ reports an error if a package named on the command line or an imported package
156
+ is not provided by any module in the build list computed from the main module's
157
+ requirements. The go command also reports an error if a module's checksum is
158
+ missing from go.sum (see Module downloading and verification). Either go.mod or
159
+ go.sum must be updated in these situations.
160
+
161
+ If invoked with -mod=mod, the go command automatically updates go.mod and
162
+ go.sum, fixing inconsistencies and adding missing requirements and checksums
163
+ as needed. If the go command finds an unfamiliar import, it looks up the
164
+ module containing that import and adds a requirement for the latest version
165
+ of that module to go.mod. In most cases, therefore, one may add an import to
166
+ source code and run "go build", "go test", or even "go list" with -mod=mod:
167
+ as part of analyzing the package, the go command will resolve the import and
168
+ update the go.mod file.
178
169
179
170
If invoked with -mod=vendor, the go command loads packages from the main
180
171
module's vendor directory instead of downloading modules to and loading packages
181
172
from the module cache. The go command assumes the vendor directory holds
182
173
correct copies of dependencies, and it does not compute the set of required
183
174
module versions from go.mod files. However, the go command does check that
184
- vendor/modules.txt (generated by ' go mod vendor' ) contains metadata consistent
175
+ vendor/modules.txt (generated by " go mod vendor" ) contains metadata consistent
185
176
with go.mod.
186
177
187
- If invoked with -mod=mod, the go command loads modules from the module cache
188
- even if there is a vendor directory present.
178
+ If the go command is not invoked with a -mod flag, and the vendor directory
179
+ is present, and the "go" version in go.mod is 1.14 or higher, the go command
180
+ will act as if it were invoked with -mod=vendor. Otherwise, the -mod flag
181
+ defaults to -mod=readonly.
189
182
190
- If the go command is not invoked with a -mod flag and the vendor directory
191
- is present and the "go" version in go.mod is 1.14 or higher, the go command
192
- will act as if it were invoked with -mod=vendor.
183
+ Note that neither "go get" nor the "go mod" subcommands accept the -mod flag.
193
184
194
185
Pseudo-versions
195
186
0 commit comments