1
1
<!-- {
2
2
"Title": "Go Modules Reference",
3
- "Subtitle": "Version of Feb 5, 2019 ",
3
+ "Subtitle": "For Go 1.15 ",
4
4
"Path": "/ref/mod"
5
5
}-->
6
- <!-- TODO(jayconrod): use <dfn> tags where meaningful.
7
- Currently, *term* is used instead, which renders to <em>term</em>. -->
6
+ <!-- TODO(golang.org/issue/33637): Write focused "guide" articles on specific
7
+ module topics and tasks. Link to those instead of the blog, which will probably
8
+ not be updated over time. -->
8
9
9
10
## Introduction {#introduction}
10
11
12
+ Modules are how Go manages dependencies.
13
+
14
+ This document is a detailed reference manual for Go's module system. For an
15
+ introduction to creating Go projects, see [ How to Write Go
16
+ Code] ( https://golang.org/doc/code.html ) . For information on using modules,
17
+ migrating projects to modules, and other topics, see the blog series starting
18
+ with [ Using Go Modules] ( https://blog.golang.org/using-go-modules ) .
19
+
11
20
## Modules, packages, and versions {#modules-overview}
12
21
13
- A [ * module* ] ( #glos-module ) is a collection of packages that are released,
14
- versioned, and distributed together. A module is identified by a [ * module
15
- path * ] ( #glos-module-path ) , which is declared in a [ ` go.mod `
16
- file ] ( #go.mod-files ) , together with information about the module's
17
- dependencies. The [ * module root directory * ] ( #glos-module-root-directory ) is the
18
- directory that contains the ` go.mod ` file. The [ * main
19
- module* ] ( #glos-main-module ) is the module containing the directory where the
20
- ` go ` command is invoked.
21
-
22
- Each [ * package * ] ( #glos-package ) within a module is a collection of source files
23
- in the same directory that are compiled together. A [ * package
24
- path * ] ( #glos-package-path ) is the module path joined with the subdirectory
25
- containing the package (relative to the module root). For example, the module
26
- ` "golang.org/x/net" ` contains a package in the directory ` "html" ` . That
27
- package's path is ` "golang.org/x/net/html" ` .
22
+ A < dfn > module</ dfn > is a collection of packages that are released, versioned ,
23
+ and distributed together. Modules may be downloaded directly from version
24
+ control repositories or from module proxy servers.
25
+
26
+ A module is identified by a [ module path ] ( #glos-module-path ) , which is declared
27
+ in a [ ` go.mod ` file] ( #go.mod-files ) , together with information about the
28
+ module's dependencies. The < dfn > module root directory</ dfn > is the directory
29
+ that contains the ` go.mod ` file. The < dfn >main module</ dfn > is the module
30
+ containing the directory where the ` go ` command is invoked.
31
+
32
+ Each < dfn >package</ dfn > within a module is a collection of source files in the
33
+ same directory that are compiled together. A < dfn >package path</ dfn > is the
34
+ module path joined with the subdirectory containing the package (relative to the
35
+ module root). For example, the module ` "golang.org/x/net" ` contains a package in
36
+ the directory ` "html" ` . That package's path is ` "golang.org/x/net/html" ` .
28
37
29
38
### Module paths {#module-path}
30
39
31
- A [ * module path* ] ( #glos-module-path ) is the canonical name for a module,
32
- declared with the [ ` module ` directive] ( #go.mod-module ) in the module's
33
- [ ` go.mod ` file] ( #glos-go.mod-file ) . A module's path is the prefix for package
34
- paths within the module.
40
+ A < dfn > module path</ dfn > is the canonical name for a module, declared with the
41
+ [ ` module ` directive] ( #go.mod-module ) in the module's [ ` go.mod `
42
+ file] ( #glos-go.mod-file ) . A module's path is the prefix for package paths within
43
+ the module.
35
44
36
45
A module path should describe both what the module does and where to find it.
37
46
Typically, a module path consists of a repository root path, a directory within
@@ -55,7 +64,7 @@ version 2 or higher).
55
64
` gopls ` . See [ Mapping versions to commits] ( #vcs-version ) and [ Module
56
65
directories within a repository] ( #vcs-dir ) .
57
66
* If the module is released at major version 2 or higher, the module path must
58
- end with a [ * major version suffix* ] ( #major-version-suffixes ) like
67
+ end with a [ major version suffix] ( #major-version-suffixes ) like
59
68
` /v2 ` . This may or may not be part of the subdirectory name. For example, the
60
69
module with path ` golang.org/x/repo/sub/v2 ` could be in the ` /sub ` or
61
70
` /sub/v2 ` subdirectory of the repository ` golang.org/x/repo ` .
@@ -67,8 +76,8 @@ module paths.
67
76
68
77
### Versions {#versions}
69
78
70
- A [ * version* ] ( #glos-version ) identifies an immutable snapshot of a module, which
71
- may be either a [ release] ( #glos-release-version ) or a
79
+ A < dfn > version</ dfn > identifies an immutable snapshot of a module, which may be
80
+ either a [ release] ( #glos-release-version ) or a
72
81
[ pre-release] ( #glos-pre-release-version ) . Each version starts with the letter
73
82
` v ` , followed by a semantic version. See [ Semantic Versioning
74
83
2.0.0] ( https://semver.org/spec/v2.0.0.html ) for details on how versions are
@@ -115,7 +124,7 @@ revisions that don't follow these conventions. However, within the main module,
115
124
the ` go ` command will automatically convert revision names that don't follow
116
125
this standard into canonical versions. The ` go ` command will also remove build
117
126
metadata suffixes (except for ` +incompatible ` ) as part of this process. This may
118
- result in a [ * pseudo-version* ] ( #glos-pseudo-version ) , a pre-release version that
127
+ result in a [ pseudo-version] ( #glos-pseudo-version ) , a pre-release version that
119
128
encodes a revision identifier (such as a Git commit hash) and a timestamp from a
120
129
version control system. For example, the command `go get -d
121
130
golang.org/x/net@daa7c041` will convert the commit hash ` daa7c041` into the
@@ -197,13 +206,13 @@ go list -m -json example.com/mod@abcd1234
197
206
198
207
### Major version suffixes {#major-version-suffixes}
199
208
200
- Starting with major version 2, module paths must have a [ * major version
201
- suffix* ] ( #glos-major-version-suffix ) like ` /v2 ` that matches the major
202
- version. For example, if a module has the path ` example.com/mod ` at ` v1.0.0 ` , it
203
- must have the path ` example.com/mod/v2 ` at version ` v2.0.0 ` .
209
+ Starting with major version 2, module paths must have a < dfn > major version
210
+ suffix</ dfn > like ` /v2 ` that matches the major version. For example, if a module
211
+ has the path ` example.com/mod ` at ` v1.0.0 ` , it must have the path
212
+ ` example.com/mod/v2 ` at version ` v2.0.0 ` .
204
213
205
- Major version suffixes implement the [ * import compatibility
206
- rule* ] ( https://research.swtch.com/vgo-import ) :
214
+ Major version suffixes implement the [ < dfn > import compatibility
215
+ rule</ dfn > ] ( https://research.swtch.com/vgo-import ) :
207
216
208
217
> If an old package and a new package have the same import path,
209
218
> the new package must be backwards compatible with the old package.
@@ -859,9 +868,6 @@ Note also that the `+incompatible` suffix may appear on
859
868
[ pseudo-versions] ( #glos-pseudo-version ) . For example,
860
869
` v2.0.1-20200722182040-012345abcdef+incompatible ` may be a valid pseudo-version.
861
870
862
- <!-- TODO(jayconrod): Is it appropriate
863
- to link to the blog here? Ideally, we would have a more detailed guide. -->
864
-
865
871
### Minimal module compatibility {#minimal-module-compatibility}
866
872
867
873
A module released at major version 2 or higher is required to have a [ major
@@ -902,8 +908,8 @@ directory in `GOPATH` mode:
902
908
* ` $vn ` is a major version suffix,
903
909
* ` $dir ` is a possibly empty subdirectory,
904
910
* If all of the following are true:
905
- * The package ` $modpath/$vn/$dir ` is not present in any relevant ` vendor `
906
- directory.
911
+ * The package ` $modpath/$vn/$dir ` is not present in any relevant [ ` vendor `
912
+ directory] ( #glos-vendor-directory ) .
907
913
* A ` go.mod ` file is present in the same directory as the importing file
908
914
or in any parent directory up to the ` $GOPATH/src ` root,
909
915
* No ` $GOPATH[i]/src/$modpath/$vn/$suffix ` directory exists (for any root
@@ -923,8 +929,8 @@ Most `go` commands may run in *Module-aware mode* or *`GOPATH` mode*. In
923
929
module-aware mode, the ` go ` command uses ` go.mod ` files to find versioned
924
930
dependencies, and it typically loads packages out of the [ module
925
931
cache] ( #glos-module-cache ) , downloading modules if they are missing. In ` GOPATH `
926
- mode, the ` go ` command ignores modules; it looks in ` vendor ` directories and in
927
- ` GOPATH ` to find dependencies.
932
+ mode, the ` go ` command ignores modules; it looks in [ ` vendor `
933
+ directories ] ( #glos-vendor-directory ) and in ` GOPATH ` to find dependencies.
928
934
929
935
Module-aware mode is active by default whenever a ` go.mod ` file is found in the
930
936
current directory or in any parent directory. For more fine-grained control, the
@@ -951,7 +957,7 @@ build, but it still stores downloaded dependencies (in `GOPATH/pkg/mod`; see
951
957
952
958
When using modules, the ` go ` command typically satisfies dependencies by
953
959
downloading modules from their sources into the module cache, then loading
954
- packages from those downloaded copies. * Vendoring* may be used to allow
960
+ packages from those downloaded copies. < dfn > Vendoring</ dfn > may be used to allow
955
961
interoperation with older versions of Go, or to ensure that all files used for a
956
962
build are stored in a single file tree.
957
963
@@ -1817,13 +1823,6 @@ uppercase letter with an exclamation mark followed by the corresponding
1817
1823
lower-case letter. This allows modules ` example.com/M ` and ` example.com/m ` to
1818
1824
both be stored on disk, since the former is encoded as ` example.com/!m ` .
1819
1825
1820
- <!-- TODO(jayconrod): This table has multi-line cells, and GitHub Flavored
1821
- Markdown doesn't have syntax for that, so we use raw HTML. Gitiles doesn't
1822
- include this table in the rendered HTML. Once x/website has a Markdown renderer,
1823
- ensure this table is readable. If the cells are too large, and it's difficult
1824
- to scan, use paragraphs or sections below.
1825
- -->
1826
-
1827
1826
<table class =" ModTable " >
1828
1827
<thead >
1829
1828
<tr>
@@ -2891,7 +2890,7 @@ of all environment variables recognized by the `go` command.
2891
2890
</li>
2892
2891
</ul>
2893
2892
<p>
2894
- See <a href="mod-commands">Module-aware commands</a> for more
2893
+ See <a href="# mod-commands">Module-aware commands</a> for more
2895
2894
information.
2896
2895
</p>
2897
2896
</td>
@@ -3232,6 +3231,11 @@ paths](#module-path).
3232
3231
[ version] ( #glos-version ) to a specific revision. See [ Mapping versions to
3233
3232
commits] ( #vcs-version ) .
3234
3233
3234
+ <a id =" glos-vendor-directory " ></a >
3235
+ ** vendor directory:** A directory named ` vendor ` that contains packages from
3236
+ other modules needed to build packages in the main module. Maintained with
3237
+ [ ` go mod vendor ` ] ( #go-mod-vendor ) . See [ Vendoring] ( #vendoring ) .
3238
+
3235
3239
<a id =" glos-version " ></a >
3236
3240
** version:** An identifier for an immutable snapshot of a module, written as the
3237
3241
letter ` v ` followed by a semantic version. See the section on
0 commit comments