You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _content/doc/go_faq.html
+34-28Lines changed: 34 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1318,49 +1318,55 @@ <h3 id="get_version">
1318
1318
How should I manage package versions using "go get"?</h3>
1319
1319
1320
1320
<p>
1321
-
Since the inception of the project, Go has had no explicit concept of package versions,
1322
-
but that is changing.
1323
-
Versioning is a source of significant complexity, especially in large code bases,
1324
-
and it has taken some time to develop an
1325
-
approach that works well at scale in a large enough
1326
-
variety of situations to be appropriate to supply to all Go users.
1321
+
The Go toolchain has a built-in system for managing versioned sets of related packages, known as <dfn>modules</dfn>.
1322
+
Modules were introduced in <ahref="/doc/go1.11#modules">Go 1.11</a> and have been ready for production use since <ahref="/doc/go1.14#introduction">1.14</a>.
1327
1323
</p>
1328
1324
1329
1325
<p>
1330
-
The Go 1.11 release adds new, experimental support
1331
-
for package versioning to the <code>go</code> command,
1332
-
in the form of Go modules.
1333
-
For more information, see the <ahref="/doc/go1.11#modules">Go 1.11 release notes</a>
1334
-
and the <ahref="/cmd/go#hdr-Modules__module_versions__and_more"><code>go</code> command documentation</a>.
1326
+
To create a project using modules, run <ahref="/ref/mod#go-mod-init"><code>go mod init</code></a>.
1327
+
This command creates a <code>go.mod</code> file that tracks dependency versions.
1328
+
</p>
1329
+
1330
+
<pre>
1331
+
go mod init example.com/project
1332
+
</pre>
1333
+
1334
+
<p>
1335
+
To add, upgrade, or downgrade a dependency, run <ahref="/ref/mod#go-get"><code>go get</code></a>:
See <ahref="/doc/tutorial/create-module.html">Tutorial: Create a module</a> for more information on getting started.
1335
1344
</p>
1336
1345
1337
1346
<p>
1338
-
Regardless of the actual package management technology,
1339
-
"go get" and the larger Go toolchain does provide isolation of
1340
-
packages with different import paths.
1341
-
For example, the standard library's <code>html/template</code> and <code>text/template</code>
1342
-
coexist even though both are "package template".
1343
-
This observation leads to some advice for package authors and package users.
1347
+
See <ahref="/doc/#developing-modules">Developing modules</a> for guides on managing dependencies with modules.
1344
1348
</p>
1345
1349
1346
1350
<p>
1347
-
Packages intended for public use should try to maintain backwards compatibility as they evolve.
1351
+
Packages within modules should maintain backward compatibility as they evolve, following the <ahref="https://research.swtch.com/vgo-import">import compatibility rule</a>:
1352
+
</p>
1353
+
1354
+
<blockquote>
1355
+
If an old package and a new package have the same import path,<br>
1356
+
the new package must be backwards compatible with the old package.
1357
+
</blockquote>
1358
+
1359
+
<p>
1348
1360
The <ahref="/doc/go1compat.html">Go 1 compatibility guidelines</a> are a good reference here:
1349
1361
don't remove exported names, encourage tagged composite literals, and so on.
1350
1362
If different functionality is required, add a new name instead of changing an old one.
1351
-
If a complete break is required, create a new package with a new import path.
1352
1363
</p>
1353
1364
1354
1365
<p>
1355
-
If you're using an externally supplied package and worry that it might change in
1356
-
unexpected ways, but are not yet using Go modules,
1357
-
the simplest solution is to copy it to your local repository.
1358
-
This is the approach Google takes internally and is supported by the
1359
-
<code>go</code> command through a technique called "vendoring".
1360
-
This involves
1361
-
storing a copy of the dependency under a new import path that identifies it as a local copy.
1362
-
See the <ahref="https://golang.org/s/go15vendor">design
1363
-
document</a> for details.
1366
+
Modules codify this with <ahref="https://semver.org/">semantic versioning</a> and semantic import versioning.
1367
+
If a break in compatibility is required, release a module at a new major version.
1368
+
Modules at major version 2 and higher require a <ahref="/ref/mod#major-version-suffixes">major version suffix</a> as part of their path (like <code>/v2</code>).
1369
+
This preserves the import compatibility rule: packages in different major versions of a module have distinct paths.
0 commit comments