blob: 885410362d57cf21a98630214ce3d502f9b1bec9 [file] [log] [blame] [view]
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -04001# Go 1.11 Modules
Bryan C. Mills75f81732018-07-16 14:15:15 -04002
thepudds720dd652018-09-23 15:05:57 -04003Go 1.11 includes preliminary support for versioned modules as proposed [here](https://golang.org/design/24301-versioned-go). Modules are an [experimental](https://research.swtch.com/vgo-accepted) opt-in feature in Go 1.11, with the hope of incorporating feedback and finalizing the feature for Go 1.12. Even though the details may change, future releases will support modules defined using Go 1.11 or `vgo`.
Bryan C. Mills75f81732018-07-16 14:15:15 -04004
thepudds720dd652018-09-23 15:05:57 -04005The recent work by the Go team on versioned modules started outside of the main Go repository with the `vgo` tool, but on July 12, 2018 support for versioned modules landed in the main repository ([announcement thread](https://groups.google.com/d/msg/golang-dev/a5PqQuBljF4/61QK4JdtBgAJ)), and [Go 1.11 was released](https://groups.google.com/d/msg/golang-nuts/-yv9VlfsFCg/lPX_DUJnEQAJ) on August 24, 2018.
thepuddsf398d512018-07-22 18:10:33 -04006
thepudds720dd652018-09-23 15:05:57 -04007Please provide feedback on modules via [existing or new issues](https://github.com/golang/go/wiki/Modules#github-issues) and via [experience reports](https://github.com/golang/go/wiki/ExperienceReports).
thepudds634f3412018-07-28 15:27:09 -04008
thepuddsfc0364b2018-08-02 22:10:15 -04009## Table of Contents
10
thepuddse4474f22018-08-02 22:26:17 -040011The remaining content on this page is organized as follows:
thepudds720dd652018-09-23 15:05:57 -040012* [Quick Start Example](https://github.com/golang/go/wiki/Modules#quick-start-example)
thepuddsfc0364b2018-08-02 22:10:15 -040013* [New Concepts](https://github.com/golang/go/wiki/Modules#new-concepts)
thepuddsfc0364b2018-08-02 22:10:15 -040014 * [Modules](https://github.com/golang/go/wiki/Modules#modules)
thepudds0e8012e2018-08-05 12:24:11 -040015 * [go.mod](https://github.com/golang/go/wiki/Modules#gomod)
thepuddsfc0364b2018-08-02 22:10:15 -040016 * [Version Selection](https://github.com/golang/go/wiki/Modules#version-selection)
thepudds14331902018-08-05 12:09:17 -040017 * [Semantic Import Versioning](https://github.com/golang/go/wiki/Modules#semantic-import-versioning)
thepudds885ec5c2018-09-24 21:46:42 -040018* [How to Use Modules](https://github.com/golang/go/wiki/Modules#how-to-use-modules)
19 * [How to Install and Activate Module Support](https://github.com/golang/go/wiki/Modules#how-to-install-and-activate-module-support)
20 * [How to Define a Module](https://github.com/golang/go/wiki/Modules#how-to-define-a-module)
21 * [How to Upgrade and Downgrade Dependencies](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies)
thepudds1adc5432018-10-05 00:18:57 -040022 * [How to Prepare for a Release (All Versions)](https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release)
23 * [How to Prepare for a Release (v2 or Higher)](https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher)
thepuddsfc0364b2018-08-02 22:10:15 -040024* [Additional Resources](https://github.com/golang/go/wiki/Modules#additional-resources)
25* [Changes Since the Initial Vgo Proposal](https://github.com/golang/go/wiki/Modules#changes-since-the-initial-vgo-proposal)
26* [GitHub Issues](https://github.com/golang/go/wiki/Modules#github-issues)
Bryan C. Mills4635c4e2018-09-28 14:16:38 -040027* [FAQs](https://github.com/golang/go/wiki/Modules#faqs)
thepuddsf4456932018-09-07 01:51:55 -040028 * [How are versions marked as incompatible?](https://github.com/golang/go/wiki/Modules#how-are-versions-marked-as-incompatible)
29 * [When do I get old behavior vs. new module-based behavior?](https://github.com/golang/go/wiki/Modules#when-do-i-get-old-behavior-vs-new-module-based-behavior)
30 * [Why does installing a tool via 'go get' fail with error 'cannot find main module'?](https://github.com/golang/go/wiki/Modules#why-does-installing-a-tool-via-go-get-fail-with-error-cannot-find-main-module)
31 * [How can I track tool dependencies for a module?](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module)
thepudds122bf9b2018-09-07 02:04:45 -040032 * [What is the status of module support in IDEs, editors and standard tools like goimports, gorename, etc.?](https://github.com/golang/go/wiki/Modules#what-is-the-status-of-module-support-in-ides-editors-and-standard-tools-like-goimports-gorename-etc)
thepudds979093a2018-09-24 22:14:19 -040033* [FAQs — Additional Control](https://github.com/golang/go/wiki/Modules#faqs--additional-control)
thepuddsf4456932018-09-07 01:51:55 -040034 * [What community tooling exists for working with modules?](https://github.com/golang/go/wiki/Modules#what-community-tooling-exists-for-working-with-modules)
thepudds122bf9b2018-09-07 02:04:45 -040035 * [When should I use the 'replace' directive?](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive)
thepuddsf4456932018-09-07 01:51:55 -040036 * [Can I work entirely outside of VCS on my local filesystem?](https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem)
37 * [How do I use vendoring with modules? Is vendoring going away?](https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away)
thepudds122bf9b2018-09-07 02:04:45 -040038 * [Are there "always on" module repositories and enterprise proxies?](https://github.com/golang/go/wiki/Modules#are-there-always-on-module-repositories-and-enterprise-proxies)
39 * [Can I control when go.mod gets updated and when the go tools use the network to satisfy dependencies?](https://github.com/golang/go/wiki/Modules#can-i-control-when-gomod-gets-updated-and-when-the-go-tools-use-the-network-to-satisfy-dependencies)
thepuddsf4456932018-09-07 01:51:55 -040040 * [How do I use modules with CI systems such as Travis or CircleCI?](https://github.com/golang/go/wiki/Modules#how-do-i-use-modules-with-ci-systems-such-as-travis-or-circleci)
thepudds979093a2018-09-24 22:14:19 -040041* [FAQs — go.mod and go.sum](https://github.com/golang/go/wiki/Modules#faqs--gomod-and-gosum)
thepudds58207da2018-09-17 09:57:11 -040042 * [Why does 'go mod tidy' record indirect and test dependencies in my 'go.mod'?](https://github.com/golang/go/wiki/Modules#why-does-go-mod-tidy-record-indirect-and-test-dependencies-in-my-gomod)
thepudds7f6103b2018-09-17 10:03:25 -040043 * [Is 'go.sum' a lock file? Why does 'go.sum' include information for module versions I am no longer using?](https://github.com/golang/go/wiki/Modules/#is-gosum-a-lock-file-why-does-gosum-include-information-for-module-versions-i-am-no-longer-using)
thepuddsd86f5aa2018-09-17 10:21:31 -040044 * [Should I still add a 'go.mod' file if I do not have any dependencies?](https://github.com/golang/go/wiki/Modules#should-i-still-add-a-gomod-file-if-i-do-not-have-any-dependencies)
thepuddse4ddd272018-10-09 10:16:32 -040045 * [Should I commit my 'go.sum' file as well as my 'go.mod' file?](https://github.com/golang/go/wiki/Modules#should-i-commit-my-gosum-file-as-well-as-my-gomod-file)
thepudds979093a2018-09-24 22:14:19 -040046* [FAQs — Semantic Import Versioning](https://github.com/golang/go/wiki/Modules#faqs--semantic-import-versioning)
47 * [Why must major version numbers appear in import paths?](https://github.com/golang/go/wiki/Modules#why-must-major-version-numbers-appear-in-import-paths)
48 * [Why are major versions v0, v1 omitted from import paths?](https://github.com/golang/go/wiki/Modules#why-are-major-versions-v0-v1-omitted-from-import-paths)
thepudds7afc40d2018-09-24 22:06:51 -040049 * [What are some implications of tagging my project with major version v0, v1, or making breaking changes with v2+?](https://github.com/golang/go/wiki/Modules#what-are-some-implications-of-tagging-my-project-with-major-version-v0-v1-or-making-breaking-changes-with-v2)
thepudds979093a2018-09-24 22:14:19 -040050* [FAQs — Minimal Version Selection](https://github.com/golang/go/wiki/Modules#faqs--minimal-version-selection)
51 * [Won't minimal version selection keep developers from getting important updates?](https://github.com/golang/go/wiki/Modules#wont-minimal-version-selection-keep-developers-from-getting-important-updates)
thepudds885ec5c2018-09-24 21:46:42 -040052* [FAQs — Possible Problems](https://github.com/golang/go/wiki/Modules#faqs-possible-problems)
thepudds122bf9b2018-09-07 02:04:45 -040053 * [Why does 'go build' require gcc, and why are prebuilt packages such as net/http not used?](https://github.com/golang/go/wiki/Modules#why-does-go-build-require-gcc-and-why-are-prebuilt-packages-such-as-nethttp-not-used)
thepuddsf4456932018-09-07 01:51:55 -040054 * [Do modules work with relative imports like `import "./subdir"`?](https://github.com/golang/go/wiki/Modules#do-modules-work-with-relative-imports-like-import-subdir)
Sylvain Laurent5b4c4e02018-10-03 17:02:28 +020055 * [Some needed files may not be present in populated vendor directory](https://github.com/golang/go/wiki/Modules#some-needed-files-may-not-be-present-in-populated-vendor-directory)
thepudds979093a2018-09-24 22:14:19 -040056* [FAQs — Miscellaneous](https://github.com/golang/go/wiki/Modules#faqs--miscellaneous)
thepudds122bf9b2018-09-07 02:04:45 -040057 * [How did the 'go mod' commands change in go1.11beta3?](https://github.com/golang/go/wiki/Modules#how-did-the-go-mod-commands-change-in-go111beta3)
thepuddsfc0364b2018-08-02 22:10:15 -040058
thepudds720dd652018-09-23 15:05:57 -040059## Quick Start Example
Bryan C. Mills75f81732018-07-16 14:15:15 -040060
thepudds720dd652018-09-23 15:05:57 -040061The details are covered in the remainder of this page, but here is a simple example of creating a module from scratch.
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -040062
thepudds720dd652018-09-23 15:05:57 -040063Create a directory outside of your GOPATH:
AJ ONeal21fc7f12018-09-20 14:06:57 -060064```
thepudds720dd652018-09-23 15:05:57 -040065$ mkdir -p /tmp/scratchpad/hello
66$ cd /tmp/scratchpad/hello
AJ ONeal21fc7f12018-09-20 14:06:57 -060067```
68
thepudds720dd652018-09-23 15:05:57 -040069Initialize a new module:
AJ ONeal21fc7f12018-09-20 14:06:57 -060070```
thepudds720dd652018-09-23 15:05:57 -040071$ go mod init github.com/you/hello
72
73go: creating new go.mod: module github.com/you/hello
74```
75
76Write your code:
77```
78$ cat <<EOF > hello.go
79package main
80
81import (
82 "fmt"
83 "rsc.io/quote"
84)
85
86func main() {
87 fmt.Println(quote.Hello())
88}
89EOF
90```
91
92Build and run:
93```
94$ go build
95$ ./hello
96
97Hello, world.
98```
99
100Note your `go.mod` file includes explicit versions for your dependencies:
101```
102$ cat go.mod
103
104module github.com/you/hello
105
106require rsc.io/quote v1.5.2
AJ ONeal21fc7f12018-09-20 14:06:57 -0600107```
108
Bryan C. Mills75f81732018-07-16 14:15:15 -0400109## New Concepts
110
thepudds345f5622018-09-06 08:55:00 -0400111These sections provide a high-level introduction to the main new concepts. For more details and rationale, please see [the official proposal document](https://golang.org/design/24301-versioned-go), this 40-minute introductory [video by Russ Cox describing the philosophy behind the design](https://www.youtube.com/watch?v=F8nrpe0XWRg&list=PLq2Nv-Sh8EbbIjQgDzapOFeVfv5bGOoPE&index=3&t=0s), or the more detailed initial [vgo blog series](https://research.swtch.com/vgo).
thepudds0e8012e2018-08-05 12:24:11 -0400112
Bryan C. Mills75f81732018-07-16 14:15:15 -0400113### Modules
114
Baruch Sadogursky43df1132018-08-28 07:18:04 -0600115A *module* is a collection of related Go packages that are versioned together as a single unit. Most often, a single version-control repository corresponds exactly to a single module, but alternatively, a single version-control repository can hold multiple modules.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400116
thepuddsdef8ca72018-08-28 17:26:39 -0400117Modules record precise dependency requirements and create reproducible builds.
thepuddscc84ca22018-08-28 17:24:14 -0400118
thepuddsdef8ca72018-08-28 17:26:39 -0400119Modules must be [semantically versioned](https://semver.org/) in the form `v(major).(minor).(patch)`, such as `v0.1.0`, `v1.2.3`, or `v3.0.1`. The leading `v` is required. If using Git, [tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) released commits with their versions. Public and private module repositories and proxies are becoming available (see for example FAQ [below](https://github.com/golang/go/wiki/Modules#are-there-always-on-module-repositories-and-enterprise-proxies)).
thepudds80d05d32018-08-13 07:38:09 -0400120
thepudds0e8012e2018-08-05 12:24:11 -0400121### go.mod
122
thepuddsdef8ca72018-08-28 17:26:39 -0400123A module is defined by a tree of Go source files with a `go.mod` file in the tree's root directory. Module source code may be located outside of GOPATH.
thepudds0e8012e2018-08-05 12:24:11 -0400124
thepuddsd293f292018-10-04 23:45:35 -0400125`go.mod` files may include comments and will look familiar to a Go programmer. Here is an example `go.mod` file defining the module `github.com/my/thing`:
thepudds0e8012e2018-08-05 12:24:11 -0400126
Bryan C. Mills75f81732018-07-16 14:15:15 -0400127```
thepuddsd293f292018-10-04 23:45:35 -0400128module github.com/my/thing
Bryan C. Mills75f81732018-07-16 14:15:15 -0400129
130require (
thepudds72779d82018-08-15 12:48:53 -0400131 github.com/some/dependency v1.2.3
thepudds725fdf32018-10-04 22:50:12 -0400132 github.com/another/dependency/v4 v4.0.0
Bryan C. Mills75f81732018-07-16 14:15:15 -0400133)
134```
135
thepuddsc53293c2018-08-15 13:05:09 -0400136There are four directives: `module`, `require`, `exclude`, `replace`.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400137
thepuddsd293f292018-10-04 23:45:35 -0400138All of the packages in a module share a common prefix -- the *module path*. The `go.mod` file defines the module path via the `module` directive. For example, if you are defining a module for two packages `example.com/my/project/foo` and `example.com/my/project/bar`, the first line in your `go.mod` file typically would be `module example.com/my/project`, and the corresponding on-disk structure could be:
139
140```
141project/
142├── go.mod
143├── bar
144│   └── bar.go
145└── foo
146 └── foo.go
147```
148
149`exclude` and `replace` directives only operate on the current (“main”) module. `exclude` and `replace` directives in modules other than the main module are ignored when building the main module. The `replace` and `exclude` statements therefore allow the main module complete control over its own build, without also being subject to complete control by dependencies. (See FAQ [below](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) for discussion of when to use a `replace` directive).
Bryan C. Mills75f81732018-07-16 14:15:15 -0400150
thepudds0e8012e2018-08-05 12:24:11 -0400151In Go source code, packages are imported using the full path including the module, for example:
thepuddse508c052018-08-15 12:57:00 -0400152 * `import "example.com/my/module/v2/pkg/foo"` to import `foo` from the v2 version of module `example.com/my/module`.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400153
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400154### Version Selection
Bryan C. Mills75f81732018-07-16 14:15:15 -0400155
thepudds0e8012e2018-08-05 12:24:11 -0400156If you add a new import to your source code that is not yet covered by a `require` in `go.mod`, any go command run (e.g., 'go build') will automatically look up the proper module and add the *highest* version of that new direct dependency to your module's `go.mod` as a `require` directive. For example, if your new import corresponds to dependency M whose latest tagged release version is `v1.2.3`, your module's `go.mod` will end up with `require M v1.2.3`, which indicates module M is a dependency with allowed version >= v1.2.3 (and < v2, given v2 is considered incompatible with v1).
thepudds5af74db2018-08-01 18:14:36 -0400157
thepudds7806bc72018-08-29 00:16:41 -0400158The *minimal version selection* algorithm is used to select the versions of all modules used in a build. For each module in a build, the version selected by minimal version selection is always the semantically *highest* of the versions explicitly listed by a `require` directive in the main module or one of its dependencies.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400159
thepudds738d95c2018-08-29 08:37:13 -0400160As an example, if your module depends on module A which has a `require D v1.0.0`, and your module also depends on module B which has a `require D v1.1.1`, then minimal version selection would choose `v1.1.1` of D to include in the build (given it is the highest listed `require` version). This selection of `v1.1.1` remains consistent even if some time later a `v1.2.0` of D becomes available. This is an example of how the modules system provides 100% reproducible builds. When ready, the module author or user might choose to upgrade to the latest available version of D or choose an explicit version for D.
thepuddsc164d5a2018-08-03 13:04:28 -0400161
thepudds91ffcea2018-08-02 23:55:11 -0400162For a brief rationale and overview of the minimal version selection algorithm, [see the "High Fidelity Builds" section](https://github.com/golang/proposal/blob/master/design/24301-versioned-go.md#update-timing--high-fidelity-builds) of the official proposal, or see the [more detailed `vgo` blog series](https://research.swtch.com/vgo).
thepudds5077edf2018-08-02 09:01:12 -0400163
thepudds6ae9d6f2018-08-15 15:52:57 -0400164To see a list of the selected module versions (including indirect dependencies), use `go list -m all`.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400165
thepudds0168d012018-08-02 22:24:15 -0400166See also the ["How to Upgrade and Downgrade Dependencies"](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies) section below and the ["How are versions marked as incompatible?"](https://github.com/golang/go/wiki/Modules#how-are-versions-marked-as-incompatible) FAQ below.
thepudds5af74db2018-08-01 18:14:36 -0400167
thepudds14331902018-08-05 12:09:17 -0400168### Semantic Import Versioning
169
thepudds01c74342018-08-25 10:49:21 -0400170For many years, the official Go FAQ has included this advice on package versioning:
171
172> "Packages intended for public use should try to maintain backwards compatibility as they evolve. The Go 1 compatibility guidelines are a good reference here: don't remove exported names, encourage tagged composite literals, and so on. If different functionality is required, add a new name instead of changing an old one. If a complete break is required, create a new package with a new import path."
173
thepudds725fdf32018-10-04 22:50:12 -0400174The last sentence is especially important — if you break compatibility, you should change the import path of your package. With Go 1.11 modules, that advice is formalized into the _import compatibility rule_:
thepudds01c74342018-08-25 10:49:21 -0400175
176> "If an old package and a new package have the same import path,
177> the new package must be backwards compatible with the old package."
178
thepudds27d36432018-10-04 23:54:43 -0400179Recall [semantic versioning](https://semver.org/) requires a major version change when a v1 or higher package makes a backwards incompatible change. The result of following both the import compatibility rule and semantic versioning is called _semantic import versioning_, where the major version is included in the import path — this ensures the import path changes any time the major version increments due to a break in compatibility.
thepudds01c74342018-08-25 10:49:21 -0400180
thepudds05465dd2018-09-23 16:22:19 -0400181As a result of semantic import versioning, code opting in to Go modules **must comply with these rules**:
thepudds23d29522018-08-25 13:00:46 -0400182* Follow [semantic versioning](https://semver.org/) (with tags such as `v1.2.3`).
thepudds656e8352018-08-21 15:32:45 -0400183* If the module is version v2 or higher, the major version of the module _must_ be included in both the module path in the `go.mod` file (e.g., `module example.com/my/mod/v2`) and the package import path (e.g., `import "example.com/my/mod/v2/foo"`).
184* If the module is version v0 or v1, do _not_ include the major version in either the module path or the import path.
185
thepudds05465dd2018-09-23 16:22:19 -0400186In general, packages with different import paths are different packages, including if different import paths are due to different major versions. Thus `example.com/my/mod/foo` is a different package than `example.com/my/mod/v2/foo`, and both may be imported in a single build, which among other benefits helps with diamond dependency problems and also allows a v1 module to be implemented in terms of its v2 replacement or vice versa.
thepudds14331902018-08-05 12:09:17 -0400187
thepudds7eb5d6f2018-09-24 22:48:23 -0400188One exception to the rules above is existing code that uses import paths starting with `gopkg.in` (such as `gopkg.in/yaml.v1` and `gopkg.in/yaml.v2`) can continue to use those forms for their module paths and import paths when opting in to modules.
189
190See the ["Module compatibility and semantic versioning"](https://tip.golang.org/cmd/go/#hdr-Module_compatibility_and_semantic_versioning) section of the tip documentation for more details on semantic import versioning.
thepuddsaa43ea42018-08-15 11:52:47 -0400191
thepudds05465dd2018-09-23 16:22:19 -0400192This section so far has been focused on code that opts in to modules. However, putting major versions in import paths for v2+ modules could create incompatibilities with older versions of Go, or with code that has not yet opted in to modules. To help with this, Go versions 1.9.7+, 1.10.3+ and 1.11 have been [updated](https://go-review.googlesource.com/c/go/+/109340) so that code built with those releases can properly consume v2+ modules without requiring modification of pre-existing code. (When relying on this updated mechanism, a package that has _not_ opted in to modules would _not_ include the major version in the import path for any imported v2+ modules. In contrast, a package that _has_ opted in to modules _must_ include the major version in the import path for any imported v2+ modules).
thepudds0e8012e2018-08-05 12:24:11 -0400193
thepudds05465dd2018-09-23 16:22:19 -0400194For the exact mechanics required to release a v2+ module, please see the ["Releasing Modules (v2 or Higher)"](https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher) section below.
thepudds14331902018-08-05 12:09:17 -0400195
thepudds885ec5c2018-09-24 21:46:42 -0400196## How to Use Modules
197
198### How to Install and Activate Module Support
thepudds720dd652018-09-23 15:05:57 -0400199
200To use modules, two install options are:
201* [Install the latest Go 1.11 release](https://golang.org/dl/).
202* [Install the Go toolchain from source](https://golang.org/doc/install/source) on the `master` branch.
203
204Once installed, you can then activate module support in one of two ways:
205* Invoke the `go` command in a directory outside of the `$GOPATH/src` tree, with a valid `go.mod` file in the current directory or any parent of it and the environment variable `GO111MODULE` unset (or explicitly set to `auto`).
206* Invoke the `go` command with `GO111MODULE=on` environment variable set.
207
thepudds885ec5c2018-09-24 21:46:42 -0400208### How to Define a Module
Bryan C. Mills75f81732018-07-16 14:15:15 -0400209
thepudds423dee02018-09-24 22:52:54 -0400210Most projects will follow the simplest approach of using a single module per repository, which typically would mean creating one `go.mod` file located in the root directory of a repository. (Multiple modules are supported in a single repository, but most often that would result in more work on an on-going basis than a single module per repository).
thepudds7eb5d6f2018-09-24 22:48:23 -0400211
thepuddsfd489a12018-08-01 02:47:49 -0400212To create a `go.mod` for an existing project:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400213
thepudds7eb5d6f2018-09-24 22:48:23 -04002141. Navigate to the root of the module's source tree outside of GOPATH:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400215
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400216 ```
thepudds7eb5d6f2018-09-24 22:48:23 -0400217 $ cd <project path outside $GOPATH/src> # e.g., cd ~/projects/hello
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400218 ```
thepudds7eb5d6f2018-09-24 22:48:23 -0400219 Note that outside of GOPATH, you do not need to set `GO111MODULE` to activate module mode.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400220
thepudds7eb5d6f2018-09-24 22:48:23 -0400221 Alternatively, if you want to work in your GOPATH:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400222
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400223 ```
thepudds7eb5d6f2018-09-24 22:48:23 -0400224 $ export GO111MODULE=on # manually active module mode
225 $ cd $GOPATH/src/<project path> # e.g., cd $GOPATH/src/you/hello
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400226 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400227
thepudds6f427542018-07-31 13:33:16 -04002282. Create the initial module definition and write it to the `go.mod` file:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400229
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400230 ```
thepudds6f434ef2018-08-13 13:29:51 -0400231 $ go mod init
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400232 ```
thepudds9bdb2722018-07-27 07:02:48 -0400233 This step converts from any existing [`dep`](https://github.com/golang/dep) `Gopkg.lock` file or from any of the other [nine total supported dependency formats](https://tip.golang.org/pkg/cmd/go/internal/modconv/?m=all#pkg-variables), adding require statements to match the existing configuration.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400234
thepudds423dee02018-09-24 22:52:54 -0400235 `go mod init` will often be able to use auxiliary data (such as VCS meta-data) to automatically determine the appropriate module path, but if `go mod init` states it can not automatically determine the module path, or if you need to otherwise override that path, you can supply the module path as follows:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400236
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400237 ```
thepudds7eb5d6f2018-09-24 22:48:23 -0400238 $ go mod init github.com/you/hello
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400239 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400240
thepuddse15da1b2018-11-25 12:58:59 -0500241 Note that if your dependencies include v2+ modules, or if you are initializing a v2+ module, then after running `go mod init` you might also need to edit your `go.mod` and `.go` code to add `/vN` to import paths and module paths as described in the ["Semantic Import Versioning"](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) section above. This applies even if `go mod init` automatically converted your dependency information from `dep` or other dependency managers. (Because of this, after running `go mod init`, you typically should not run `go mod tidy` until you have successfully run `go build ./...` or similar, which is the sequence shown in this section).
242
thepudds74241a12018-07-31 13:31:00 -04002433. Build the module. This will automatically add missing or unconverted dependencies as needed to satisfy imports for this particular build invocation:
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400244
245 ```
thepudds74241a12018-07-31 13:31:00 -0400246 $ go build ./...
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400247 ```
thepudds6f427542018-07-31 13:33:16 -04002484. Test the module as configured to ensure that it works with the selected versions:
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400249
250 ```
251 $ go test ./...
252 ```
253
thepuddsd5e66ee2018-08-15 13:20:27 -04002545. (Optional) Run the tests for your module plus the tests for all direct and indirect dependencies to check for incompatibilities:
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400255
256 ```
thepudds4b3d8932018-07-27 06:50:53 -0400257 $ go test all
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400258 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400259
thepudds0168d012018-08-02 22:24:15 -0400260Prior to tagging a release, see the ["How to Prepare for a Release"](https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release) section below.
thepudds74241a12018-07-31 13:31:00 -0400261
thepudds432bda32018-08-07 05:22:33 -0400262For more information on all of these topics, the primary entry point to the official modules documentation is [available on tip.golang.org](https://tip.golang.org/cmd/go/#hdr-Modules__module_versions__and_more).
263
thepudds88919642018-08-02 21:51:04 -0400264## How to Upgrade and Downgrade Dependencies
Bryan C. Mills75f81732018-07-16 14:15:15 -0400265
thepudds4b3d8932018-07-27 06:50:53 -0400266Day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get', which will automatically update the `go.mod` file.
267
thepudds61c12ce2018-07-27 08:04:34 -0400268In addition, go commands like 'go build', 'go test', or even 'go list' will automatically add new dependencies as needed to satisfy imports (updating `go.mod` and downloading the new dependencies).
thepuddsb05a8442018-07-26 08:18:42 -0400269
thepudds927647d2018-07-26 09:03:39 -0400270To upgrade to the latest version for all transitive dependencies of the current module:
thepudds5af74db2018-08-01 18:14:36 -0400271 * run `go get -u` to use the latest *minor or patch* releases
272 * run `go get -u=patch` to use the latest *patch* releases
thepuddsee879c52018-07-25 12:08:34 -0400273
thepudds8f75c6c2018-08-01 09:18:37 -0400274To upgrade or downgrade to a more specific version, 'go get' allows version selection to be overridden by adding an @version suffix or "module query" to the package argument, such as `go get github.com/gorilla/mux@v1.6.2`, `go get github.com/gorilla/mux@e3702bed2`, or `go get github.com/gorilla/mux@'<v1.6.2'`.
275
thepuddscf232d22018-11-02 17:58:22 -0400276`go get github.com/gorilla/mux` updates to the latest version with a [semver](https://semver.org/) tag. (This is equivalent to `go get github.com/gorilla/mux@latest` — in other words, `@latest` is the default if no `@` version is specified).
277
278Using a branch name such as `go get github.com/gorilla/mux@master` is one way to obtain the latest commit regardless of whether or not it has a semver tag.
thepuddsff8f0f82018-08-25 13:07:59 -0400279
280In general, module queries that do not resolve to a semver tag will be recorded as [pseudo-versions](https://tip.golang.org/cmd/go/#hdr-Pseudo_versions) in the `go.mod` file.
thepudds72779d82018-08-15 12:48:53 -0400281
thepudds90d81be2018-08-26 12:21:40 -0400282Modules are capable of consuming packages that have not yet opted into modules. Modules can also consume packages that do not yet have any proper semver tags (in which case they will be recorded using pseudo-versions in `go.mod`).
283
thepudds8f75c6c2018-08-01 09:18:37 -0400284See the ["Module-aware go get"](https://tip.golang.org/cmd/go/#hdr-Module_aware_go_get) and ["Module queries"](https://tip.golang.org/cmd/go/#hdr-Module_queries) sections of the tip documentation for more information on the topics here.
thepuddsb05a8442018-07-26 08:18:42 -0400285
thepuddsd5e66ee2018-08-15 13:20:27 -0400286After upgrading or downgrading any dependencies, you may then want to run the tests again for all packages in your build (including direct and indirect dependencies) to check for incompatibilities:
x1ah2e74f822018-11-22 14:45:57 +0800287
thepuddsee879c52018-07-25 12:08:34 -0400288 ```
thepudds4b3d8932018-07-27 06:50:53 -0400289 $ go test all
thepuddsee879c52018-07-25 12:08:34 -0400290 ```
thepuddsf398d512018-07-22 18:10:33 -0400291
thepudds88919642018-08-02 21:51:04 -0400292## How to Prepare for a Release
thepudds61aba5c2018-07-27 07:10:40 -0400293
thepudds05465dd2018-09-23 16:22:19 -0400294### Releasing Modules (All Versions)
295
thepuddsdf13b282018-08-01 00:38:40 -0400296Best practices for creating a release of a module are expected to emerge as part of the initial modules experiment. Many of these might end up being automated by a [future 'go release' tool](https://github.com/golang/go/issues/26420).
thepudds61aba5c2018-07-27 07:10:40 -0400297
thepudds88919642018-08-02 21:51:04 -0400298Some current suggested best practices to consider prior to tagging a release:
thepudds61aba5c2018-07-27 07:10:40 -0400299
thepudds885ec5c2018-09-24 21:46:42 -0400300* Run `go mod tidy` to possibly prune any extraneous requirements (as described [here](https://tip.golang.org/cmd/go/#hdr-Maintaining_module_requirements)) and also ensure your current go.mod reflects all possible build tags/OS/architecture combinations (as described [here](https://github.com/golang/go/issues/25971#issuecomment-399091682)).
thepuddscb4e8b52018-08-28 15:01:45 -0400301 * In contrast, other commands like `go build` and `go test` will not remove dependencies from `go.mod` that are no longer required and only update `go.mod` based on the current build invocation's tags/OS/architecture.
thepuddsf918d5c2018-07-31 13:36:22 -0400302
thepuddsd8674ab2018-08-15 15:22:10 -0400303* Run `go test all` to test your module (including running the tests for your direct and indirect dependencies) as a way of validating that the currently selected packages versions are compatible.
thepuddsd5e66ee2018-08-15 13:20:27 -0400304 * The number of possible version combinations is exponential in the number of modules, so in general you cannot expect your dependencies to have tested against all possible combinations of their dependencies.
thepudds61aba5c2018-07-27 07:10:40 -0400305 * As part of the modules work, `go test all` has been [re-defined to be more useful](https://research.swtch.com/vgo-cmd) to include all the packages in the current module, plus all the packages they depend on through a sequence of one or more imports, while excluding packages that don't matter in the current module.
thepudds61aba5c2018-07-27 07:10:40 -0400306
thepuddse4ddd272018-10-09 10:16:32 -0400307* Ensure your `go.sum` file is committed along with your `go.mod` file. See [FAQ below](https://github.com/golang/go/wiki/Modules#should-i-commit-my-gosum-file-as-well-as-my-gomod-file) for more details and rationale.
thepuddsabb30ff2018-08-15 09:32:02 -0400308
thepudds05465dd2018-09-23 16:22:19 -0400309### Releasing Modules (v2 or Higher)
310
311If you are releasing a v2 or higher module, please first review the discussion in the ["Semantic Import Versioning" ](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) section above, which includes why major versions are included in the module path and import path for v2+ modules, as well as how Go versions 1.9.7+ and 1.10.3+ have been updated to simplify that transition.
312
313There are two ways to release a v2 or higher module. Using the example of creating a `v3.0.0` release, the two options are:
314
thepudds725fdf32018-10-04 22:50:12 -04003151. **Major branch**: Update the `go.mod` file to include a `/v3` at the end of the module path in the `module` directive (e.g., `module github.com/my/module/v3`). Update import statements within the module to also use `/v3` (e.g., `import "github.com/my/module/v3/foo"`). Tag the release with `v3.0.0`.
thepudds05465dd2018-09-23 16:22:19 -0400316 * Go versions 1.9.7+, 1.10.3+, and 1.11 are able to properly consume and build a v2+ module created using this approach without requiring updates to consumer code that has not yet opted in to modules (as described in the the ["Semantic Import Versioning"](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) section above).
317 * A community tool [github.com/marwan-at-work/mod](https://github.com/marwan-at-work/mod) helps automate this procedure. See the [repository](https://github.com/marwan-at-work/mod) or the [community tooling FAQ](https://github.com/golang/go/wiki/Modules#what-community-tooling-exists-for-working-with-modules) below for an overview.
318 * To avoid confusion with this approach, consider putting the `v3.*.*` commits for the module on a separate v3 branch.
thepuddsccab1c32018-11-20 10:18:36 -0500319 * **Note:** creating a new branch is _not_ required. If instead you have been previously releasing on master and would prefer to tag `v3.0.0` on master, that is a viable option. (If you do decide to tag `v3.0.0` on master, you can consider creating a v2 branch for any future v2 bug fixes).
thepudds05465dd2018-09-23 16:22:19 -0400320
Bryan C. Mills4635c4e2018-09-28 14:16:38 -04003212. **Major subdirectory**: Create a new `v3` subdirectory (e.g., `my/module/v3`) and place a new `go.mod` file in that subdirectory. The module path must end with `/v3`. Copy or move the code into the `v3` subdirectory. Update import statements within the module to also use `/v3` (e.g., `import "github.com/my/module/v3/foo"`). Tag the release with `v3.0.0`.
thepudds05465dd2018-09-23 16:22:19 -0400322 * This provides greater backwards compatibility. In particular, Go versions older than 1.9.7 and 1.10.3 are also able to properly consume and build a v2+ module created using this approach.
thepuddsd0ef5ad2018-08-15 12:07:14 -0400323
Bryan C. Mills4635c4e2018-09-28 14:16:38 -0400324See https://research.swtch.com/vgo-module for a more in-depth discussion of these alternatives.
325
thepudds8390d032018-07-22 23:37:00 -0400326## Additional Resources
thepuddsf398d512018-07-22 18:10:33 -0400327
thepudds8390d032018-07-22 23:37:00 -0400328### Documentation and Proposal
329
thepuddsfd489a12018-08-01 02:47:49 -0400330* Official documentation:
331 * Latest [HTML documentation for modules on tip.golang.org](https://tip.golang.org/cmd/go/#hdr-Modules__module_versions__and_more)
332 * Run `go help modules` for more about modules. (This is the main entry point for modules topics via `go help`)
333 * Run `go help mod` for more about the `go mod` command.
334 * Run `go help module-get` for more about the behavior of `go get` when in module-aware mode.
335 * Run `go help goproxy` for more about the module proxy, including a pure file-based option via a `file:///` URL.
thepudds592f7432018-07-26 10:03:47 -0400336* The initial ["Go & Versioning"](https://research.swtch.com/vgo) series of blog posts on `vgo` by Russ Cox (first posted February 20, 2018)
337* Official [golang.org blog post introducing the proposal](https://blog.golang.org/versioning-proposal) (March 26, 2018)
338 * This provides a more succinct overview of the proposal than the full `vgo` blog series, along with some of the history and process behind the proposal
thepudds8390d032018-07-22 23:37:00 -0400339* Official [Versioned Go Modules Proposal](https://golang.org/design/24301-versioned-go) (last updated March 20, 2018)
340
341### Introductory Material
342
thepudds7afc40d2018-09-24 22:06:51 -0400343* Example based 35 minute introductory video ["What are Go modules and how do I use them?"](https://www.youtube.com/watch?v=6MbIzJmLz6Q&list=PL8QGElREVyDA2iDrPNeCe8B1u7li5S6ep&index=5&t=0s) ([slides](https://talks.godoc.org/github.com/myitcv/talks/2018-08-15-glug-modules/main.slide#1)) by Paul Jolly (August 15, 2018)
thepudds8390d032018-07-22 23:37:00 -0400344* Introductory blog post ["Taking Go Modules for a Spin"](https://dave.cheney.net/2018/07/14/taking-go-modules-for-a-spin) by Dave Cheney (July 14, 2018)
thepudds8390d032018-07-22 23:37:00 -0400345* Introductory [Go Meetup slides on modules](https://docs.google.com/presentation/d/1ansfXN8a_aVL-QuvQNY7xywnS78HE8aG7fPiITNQWMM/edit#slide=id.g3d87f3177d_0_0) by Chris Hines (July 16, 2018)
thepudds7afc40d2018-09-24 22:06:51 -0400346* Introductory 40 minute video ["The Principles of Versions in Go"](https://www.youtube.com/watch?v=F8nrpe0XWRg&list=PLq2Nv-Sh8EbbIjQgDzapOFeVfv5bGOoPE&index=3&t=0s) from GopherCon Singapore by Russ Cox (May 2, 2018)
thepudds8390d032018-07-22 23:37:00 -0400347 * Succinctly covers the philosophy behind the design of versioned Go modules, including the three core principles of "Compatibility", "Repeatability", and "Cooperation"
348
349### Additional Material
350
thepudds345f5622018-09-06 08:55:00 -0400351* Blog post ["Using Go modules with vendor support on Travis CI"](https://arslan.io/2018/08/26/using-go-modules-with-vendor-support-on-travis-ci/) by Fatih Arslan (August 26, 2018)
352* Blog post ["Go Modules and CircleCI"](https://medium.com/@toddkeech/go-modules-and-circleci-c0d6fac0b000) by Todd Keech (July 30, 2018)
thepudds592f7432018-07-26 10:03:47 -0400353* Blog post ["The vgo proposal is accepted. Now what?"](https://research.swtch.com/vgo-accepted) by Russ Cox (May 29, 2018)
thepudds8390d032018-07-22 23:37:00 -0400354 * Includes summary of what it means that versioned modules are currently an experimental opt-in feature
thepudds345f5622018-09-06 08:55:00 -0400355* Blog post on [how to build go from tip and start using go modules](https://carolynvanslyck.com/blog/2018/07/building-go-from-source/) by Carolyn Van Slyck (July 16, 2018)
thepudds8390d032018-07-22 23:37:00 -0400356
thepudds88919642018-08-02 21:51:04 -0400357## Changes Since the Initial Vgo Proposal
358
thepuddsa7d30542018-08-03 18:02:38 -0400359As part of the proposal, prototype, and beta processes, there have been over 400 issues created by the overall community. Please continue to supply feedback.
360
361Here is a partial list of some of the larger changes and improvements, almost all of which were primarily based on feedback from the community:
thepudds88919642018-08-02 21:51:04 -0400362
363* Top-level vendor support was retained rather than vgo-based builds ignoring vendor directories entirely ([discussion](https://groups.google.com/d/msg/golang-dev/FTMScX1fsYk/uEUSjBAHAwAJ), [CL](https://go-review.googlesource.com/c/vgo/+/118316))
thepudds0168d012018-08-02 22:24:15 -0400364* Backported minimal module-awareness to allow older Go versions 1.9.7+ and 1.10.3+ to more easily consume modules for v2+ projects ([discussion](https://github.com/golang/go/issues/24301#issuecomment-371228742), [CL](https://golang.org/cl/109340))
thepudds42f1dea2018-08-03 18:07:15 -0400365* Allowed vgo to use v2+ tags by default for pre-existing packages did not yet have a go.mod (recent update in related behavior described [here](https://github.com/golang/go/issues/25967#issuecomment-407567904))
thepudds0168d012018-08-02 22:24:15 -0400366* Added support via command `go get -u=patch` to update all transitive dependencies to the latest available patch-level versions on the same minor version ([discussion](https://research.swtch.com/vgo-cmd), [documentation](https://tip.golang.org/cmd/go/#hdr-Module_aware_go_get))
thepudds88919642018-08-02 21:51:04 -0400367* Additional control via environmental variables (e.g., GOFLAGS in [#26585](https://github.com/golang/go/issues/26585), [CL](https://go-review.googlesource.com/c/go/+/126656))
thepudds55989cc2018-08-05 20:39:02 -0400368* Finer grain control on whether or not go.mod is allowed to be updated, how vendor directory is used, and whether or not network access is allowed (e.g., -mod=readonly, -mod=vendor, GOPROXY=off; related [CL](https://go-review.googlesource.com/c/go/+/126696) for recent change)
thepudds0168d012018-08-02 22:24:15 -0400369* Added more flexible replace directives ([CL](https://go-review.googlesource.com/c/vgo/+/122400))
370* Added additional ways to interrogate modules (for human consumption, as well as for better editor / IDE integration)
371* The UX of the go CLI has continued to be refined based on experiences so far (e.g., [#26581](https://github.com/golang/go/issues/26581), [CL](https://go-review.googlesource.com/c/go/+/126655))
thepudds856618c2018-09-07 00:20:35 -0400372* Additional support for warming caches for use cases such as CI or docker builds via `go mod download` ([#26610](https://github.com/golang/go/issues/26610#issuecomment-408654653))
thepudds88919642018-08-02 21:51:04 -0400373* **Most likely**: better support for installing specific versions of programs to GOBIN ([#24250](https://github.com/golang/go/issues/24250#issuecomment-377553022))
374
thepudds88919642018-08-02 21:51:04 -0400375## GitHub Issues
thepudds8390d032018-07-22 23:37:00 -0400376
thepuddsdf13b282018-08-01 00:38:40 -0400377* [Currently open module issues](https://golang.org/issues?q=is%3Aopen+is%3Aissue+label:modules)
thepudds7eb5d6f2018-09-24 22:48:23 -0400378* [Closed module issues](https://github.com/golang/go/issues?q=is%3Aclosed+is%3Aissue+label%3Amodules+sort%3Aupdated-desc)
379* [Closed vgo issues](https://github.com/golang/go/issues?q=-label%3Amodules+vgo+is%3Aclosed+sort%3Aupdated-desc)
thepuddse01f40e2018-08-01 01:05:48 -0400380* Submit a [new module issue](https://github.com/golang/go/issues/new?title=cmd%2Fgo%3A%20%3Cfill%20this%20in%3E) using 'cmd/go:' as the prefix
381
thepudds7afc40d2018-09-24 22:06:51 -0400382## FAQs — Most Common
thepuddse01f40e2018-08-01 01:05:48 -0400383
384### How are versions marked as incompatible?
385
thepuddsd9442592018-08-02 09:44:49 -0400386The `require` directive allows any module to declare that it should be built with version >= x.y.z of a dependency D (which may be specified due to incompatibilities with version < x.y.z of module D). Empirical data suggests [this is the dominant form of constraints used in `dep` and `cargo`](https://twitter.com/_rsc/status/1022590868967116800). In addition, the top-level module in the build can `exclude` specific versions of dependencies or `replace` other modules with different code. See the full proposal for [more details and rationale](https://github.com/golang/proposal/blob/master/design/24301-versioned-go.md).
thepuddse01f40e2018-08-01 01:05:48 -0400387
thepudds601d2312018-08-06 23:26:24 -0400388One of the key goals of the versioned modules proposal is to add a common vocabulary and semantics around versions of Go code for both tools and developers. This lays a foundation for future capabilities to declare additional forms of incompatibilities, such as possibly:
thepudds0bb0f8d2018-08-01 01:51:19 -0400389* declaring deprecated versions as [described](https://research.swtch.com/vgo-module) in the initial `vgo` blog series
thepuddse01f40e2018-08-01 01:05:48 -0400390* declaring pair-wise incompatibility between modules in an external system as discussed for example [here](https://github.com/golang/go/issues/24301#issuecomment-392111327) during the proposal process
thepudds00c21b72018-09-07 01:00:11 -0400391* declaring pair-wise incompatible versions or insecure versions of a module after a release has been published. See for example the on-going discussion in [#24031](https://github.com/golang/go/issues/24031#issuecomment-407798552) and [#26829](https://github.com/golang/go/issues/26829)
thepuddse01f40e2018-08-01 01:05:48 -0400392
thepudds758e6082018-08-25 15:02:30 -0400393### When do I get old behavior vs. new module-based behavior?
394
395In general, modules are opt-in for Go 1.11, so by design old behavior is preserved by default.
396
397Summarizing when you get the old 1.10 status quo behavior vs. the new opt-in modules-based behavior:
398
399* Inside GOPATH — defaults to old 1.10 behavior (ignoring modules)
thepudds038b77e2018-08-28 12:20:18 -0400400* Outside GOPATH while inside a file tree with a `go.mod` — defaults to modules behavior
thepudds758e6082018-08-25 15:02:30 -0400401* GO111MODULE environment variable:
thepuddse24c7712018-08-25 15:09:57 -0400402 * unset or `auto` — default behavior above
403 * `on` — force module support on regardless of directory location
404 * `off` — force module support off regardless of directory location
thepudds758e6082018-08-25 15:02:30 -0400405
thepudds320b6242018-09-06 23:52:46 -0400406### Why does installing a tool via `go get` fail with error `cannot find main module`?
thepudds758e6082018-08-25 15:02:30 -0400407
thepudds868cbbc2018-09-06 23:59:14 -0400408This occurs when you have set `GO111MODULE=on`, but are not inside of a file tree with a `go.mod` when you run `go get`.
thepudds758e6082018-08-25 15:02:30 -0400409
thepudds856618c2018-09-07 00:20:35 -0400410The simplest solution is to leave `GO111MODULE` unset (or equivalently explicitly set to `GO111MODULE=auto`), which avoids this error.
thepuddse24c7712018-08-25 15:09:57 -0400411
thepudds868cbbc2018-09-06 23:59:14 -0400412Recall one of the primary reason modules exist is to record precise dependency information. This dependency information is written to your current `go.mod`. If you are not inside of a file tree with a `go.mod` but you have told the `go get` command to operate in module mode by setting `GO111MODULE=on`, then running `go get` will result in the error `cannot find main module` because there is no `go.mod` available to record dependency information.
thepuddse24c7712018-08-25 15:09:57 -0400413
thepudds868cbbc2018-09-06 23:59:14 -0400414Solution alternatives include:
thepudds586c5c02018-08-25 21:37:33 -0400415
thepudds856618c2018-09-07 00:20:35 -04004161. Leave `GO111MODULE` unset (the default, or explicitly set `GO111MODULE=auto`), which results in friendlier behavior. This will give you Go 1.10 behavior when you are outside of a module and hence will avoid `go get` reporting `cannot find main module`.
thepudds758e6082018-08-25 15:02:30 -0400417
thepudds320b6242018-09-06 23:52:46 -04004182. Leave `GO111MODULE=on`, but as needed disable modules temporarily and enable Go 1.10 behavior during `go get`, such as via `GO111MODULE=off go get example.com/cmd`. This can be turned into a simple script or shell alias such as `alias oldget='GO111MODULE=off go get'`
419
4203. Create a temporary `go.mod` file that is then discarded. This has been automated by a [simple shell script](https://gist.github.com/rogpeppe/7de05eef4dd774056e9cf175d8e6a168) by [@rogpeppe](https://github.com/rogpeppe). This script allows version information to optionally be supplied (usage: `vgoget example.com/cmd[@version]`).
421
4224. Create a `go.mod` you use to track your globally installed tools, such as in `~/global-tools/go.mod`, and `cd` to that directory prior to running `go get` or `go install` for any globally installed tools.
423
4245. Create a `go.mod` for each tool in separate directories, such as `~/tools/gorename/go.mod` and `~/tools/goimports/go.mod`, and `cd` to that appropriate directory prior to running `go get` or `go install` for the tool.
425
426This current limitation will be resolved. However, the primary issue is that modules are currently opt-in, and a full solution will likely wait until GO111MODULE=on becomes the default behavior. See [#24250](https://github.com/golang/go/issues/24250#issuecomment-377553022) for more discussion, including this comment:
427
428> This clearly must work eventually. The thing I'm not sure about is exactly what this does as far as the version is concerned: does it create a temporary module root and go.mod, do the install, and then throw it away? Probably. But I'm not completely sure, and for now I didn't want to confuse people by making vgo do things outside go.mod trees. Certainly the eventual go command integration has to support this.
429
430This FAQ has been discussing tracking _globally_ installed tools.
thepudds758e6082018-08-25 15:02:30 -0400431
thepudds93bae8f2018-08-25 22:39:46 -0400432If instead you want to track the tools required by a _specific_ module, see the next FAQ.
thepudds758e6082018-08-25 15:02:30 -0400433
434### How can I track tool dependencies for a module?
435
436If you:
437 * want to use a go-based tool (e.g. stringer) while working on a module, and
438 * want to ensure that everyone is using the same version of that tool while tracking the tool's version in your module's `go.mod` file
439
440then one currently recommended approach is to add a `tools.go` file to your module with a `// +build tools` build constraint as shown in [this comment in #25922](https://github.com/golang/go/issues/25922#issuecomment-412992431).
441
442The brief rationale (also from #25922):
443
444> I think the tools.go file is in fact the best practice for tool dependencies, certainly for Go 1.11.
thepuddse24c7712018-08-25 15:09:57 -0400445>
thepudds758e6082018-08-25 15:02:30 -0400446> I like it because it does not introduce new mechanisms.
thepuddse24c7712018-08-25 15:09:57 -0400447>
thepudds758e6082018-08-25 15:02:30 -0400448> It simply reuses existing ones.
449
thepuddsb488c3a2018-09-02 12:26:20 -0400450### What is the status of module support in IDEs, editors and standard tools like goimports, gorename, etc?
451
452Support for modules is starting to land in editors and IDEs.
453
454For example:
455* **GoLand**: currently has full support for modules outside and inside GOPATH, including completion, syntax analysis, refactoring, navigation as described [here](https://blog.jetbrains.com/go/2018/08/24/goland-2018-2-2-is-here/).
thepudds5dd795a2018-09-24 23:57:51 -0400456* **VS Code**: work is in progress and looking for contributors to help. Tracking issue is [#1532](https://github.com/Microsoft/vscode-go/issues/1532). An initial beta is described in the [VS Code module status wiki page](https://github.com/Microsoft/vscode-go/wiki/Go-modules-support-in-Visual-Studio-Code).
thepuddsb488c3a2018-09-02 12:26:20 -0400457* **Atom with go-plus**: tracking issue is [#761](https://github.com/joefitzgerald/go-plus/issues/761).
458* **vim with vim-go**: initial support for syntax highlighting and formatting `go.mod` has [landed](https://github.com/fatih/vim-go/pull/1931). Broader support tracked in [#1906](https://github.com/fatih/vim-go/issues/1906).
459* **emacs with go-mode.el**: tracking issue in [#237](https://github.com/dominikh/go-mode.el/issues/237).
460
461The status of other tools such as goimports, guru, gorename and similar tools is being tracked in an umbrella issue [#24661]( https://github.com/golang/go/issues/24661). Please see that umbrella issue for latest status.
462
463Some tracking issues for particular tools includes:
464* **gocode**: tracking issue in [mdempsky/gocode/#46](https://github.com/mdempsky/gocode/issues/46). Note that `nsf/gocode` is recommending people migrate off of `nsf/gocode` to `mdempsky/gocode`.
thepuddsb488c3a2018-09-02 12:26:20 -0400465* **go-tools** (tools by dominikh such as staticcheck, megacheck, gosimple): sample tracking issue [dominikh/go-tools#328](https://github.com/dominikh/go-tools/issues/328).
466
467In general, even if your editor, IDE or other tools have not yet been made module aware, much of their functionality should work with modules if you are using modules inside GOPATH and do `go mod vendor` (because then the proper dependencies should be picked up via GOPATH).
468
thepudds636e98d2018-09-07 01:38:02 -0400469The full fix is to move programs that load packages off of `go/build` and onto `golang.org/x/tools/go/packages`, which understands how to locate packages in a module-aware manner. This will likely eventually become `go/packages`.
470
thepudds7afc40d2018-09-24 22:06:51 -0400471## FAQs — Additional Control
472
thepudds636e98d2018-09-07 01:38:02 -0400473### What community tooling exists for working with modules?
474
475The community is starting to build tooling on top of modules. For example:
476
477* [github.com/rogpeppe/gohack](https://github.com/rogpeppe/gohack)
478 * A new community tool to automate and greatly simplify `replace` and multi-module workflows, including allowing you to easily modify one of your dependencies
479 * For example, `gohack example.com/some/dependency` automatically clones the appropriate repository and adds the necessary `replace` directives to your `go.mod`
480 * Remove all gohack replace statements with `gohack -u`
481 * The project is continuing to expand to make other module-related workflows easier
482* [github.com/marwan-at-work/mod](https://github.com/marwan-at-work/mod)
483 * Command line tool to automatically upgrade/downgrade major versions for modules
484 * Automatically adjusts `go.mod` files and related import statements in go source code
485 * Helps with upgrades, or when first opting in to modules with a v2+ package
486* [github.com/goware/modvendor](https://github.com/goware/modvendor)
487 * Helps copy additional files into the `vendor/` folder, such as shell scripts, .cpp files, .proto files, etc.
488
489### When should I use the replace directive?
490
thepudds0adc8292018-09-07 10:05:32 -0400491* As described in the ['go.mod' concepts section above](https://github.com/golang/go/wiki/Modules#gomod), `replace` directives provide additional control in the top-level `go.mod` for what is actually used to satisfy a dependency found in the Go source or go.mod files, while `replace` directives in modules other than the main module are ignored when building the main module.
492* The `replace` directive allows you to supply another import path that might be another module located in VCS (GitHub or elsewhere), or on your local filesystem with a relative or absolute file path. The new import path from the `replace` directive is used without needing to update the import paths in the actual source code.
493* One sample use case is if you need to fix or investigate something in a dependency, you can have a local fork and add the something like the following in your top-level `go.mod`:
thepudds636e98d2018-09-07 01:38:02 -0400494 * `replace example.com/original/import/path => /your/forked/import/path`
495* `replace` also allows the top-level module control over the exact version used for a dependency, such as:
thepudds12d4be02018-09-24 00:23:47 -0400496 * `replace example.com/some/dependency => example.com/some/dependency v1.2.3`
thepudds636e98d2018-09-07 01:38:02 -0400497* `replace` also can be used to inform the go tooling of the relative or absolute on-disk location of modules in a multi-module project, such as:
498 * `replace example.com/project/foo => ../foo`
thepudds343420b2018-09-07 09:59:22 -0400499* **Note**: in general, you can specify a version to the left of the `=>` in a replace directive, but typically it is less sensitive to change if you omit that (e.g., as done in the examples above).
thepudds636e98d2018-09-07 01:38:02 -0400500* See the [tip documentation](https://tip.golang.org/cmd/go/#hdr-Edit_go_mod_from_tools_or_scripts) for more details.
thepudds122bf9b2018-09-07 02:04:45 -0400501* [github.com/rogpeppe/gohack](https://github.com/rogpeppe/gohack) makes these types of workflows much easier. See the [repository](https://github.com/rogpeppe/gohack) or the immediately prior FAQ for an overview.
thepudds636e98d2018-09-07 01:38:02 -0400502
503### Can I work entirely outside of VCS on my local filesystem?
504
505Yes. VCS is not required.
506
thepudds32421f02018-09-07 02:14:16 -0400507This is very simple if you have a single module you want to edit at a time, and you can place the file tree containing the single `go.mod` in a convenient location.
thepudds636e98d2018-09-07 01:38:02 -0400508
509If you want to have multiple inter-related modules on your local disk that you want to edit at the same time, then `replace` directives are one approach. Here is a sample `go.mod` that uses a `replace` with a relative path to point the `hello` module at the on-disk location of the `goodbye` module (without relying on any VCS):
510
511```
512module example.com/me/hello
513
514require (
515 example.com/me/goodbye v0.0.0
516)
517
518replace example.com/me/goodbye => ../goodbye
519```
Bryan C. Mills4635c4e2018-09-28 14:16:38 -0400520As shown in this example, if outside of VCS you can use `v0.0.0` as the version in the `require` directive. Note that the `require` directive is needed. (`replace foo => ../foo` doesn't yet work without a corresponding `require foo v0.0.0`: see [#26241](https://golang.org/issue/26241).)
thepudds636e98d2018-09-07 01:38:02 -0400521
522A small runnable example is shown in this [thread](https://groups.google.com/d/msg/golang-nuts/1nYoAMFZVVM/eppaRW2rCAAJ).
thepuddsb488c3a2018-09-02 12:26:20 -0400523
thepudds00c21b72018-09-07 01:00:11 -0400524### How do I use vendoring with modules? Is vendoring going away?
thepuddse01f40e2018-08-01 01:05:48 -0400525
thepudds00c21b72018-09-07 01:00:11 -0400526The initial series of `vgo` blog posts did propose dropping vendoring entirely, but [feedback](https://groups.google.com/d/msg/golang-dev/FTMScX1fsYk/uEUSjBAHAwAJ) from the community resulted in retaining support for vendoring.
527
528In brief, to use vendoring with modules:
529* `go mod vendor` resets the main module's vendor directory to include all packages needed to build and test all of the module's packages based on the state of the go.mod files and Go source code.
530* By default, go commands like `go build` ignore the vendor directory when in module mode.
531* The `-mod=vendor` flag (e.g., `go build -mod=vendor`) instructs the go commands to use the main module's top-level vendor directory to satisfy dependencies. The go commands in this mode therefore ignore the dependency descriptions in go.mod and assume that the vendor directory holds the correct copies of dependencies. Note that only the main module's top-level vendor directory is used; vendor directories in other locations are still ignored.
Filippo Valsorda6a7f7732018-09-08 01:55:06 -0400532* Some people will want to routinely opt-in to vendoring by setting a `GOFLAGS=-mod=vendor` environment variable.
thepudds00c21b72018-09-07 01:00:11 -0400533
534Older versions of Go such as 1.10 understand how to consume a vendor directory created by `go mod vendor`, so vendoring is one way to provide dependencies to older versions of Go that do not fully understand modules.
535
536If you are considering using vendoring, it is worthwhile to read the ["Modules and vendoring"](https://tip.golang.org/cmd/go/#hdr-Modules_and_vendoring) and ["Make vendored copy of dependencies"](https://tip.golang.org/cmd/go/#hdr-Make_vendored_copy_of_dependencies) sections of the tip documentation.
537
thepudds122bf9b2018-09-07 02:04:45 -0400538### Are there "always on" module repositories and enterprise proxies?
539
540Publicly hosted "always on" immutable module repositories and optional privately hosted proxies and repositories are becoming available.
541
542For example:
543* [Project Athens](https://github.com/gomods/athens): Open source project in the works and looking for contributors.
Alexei Vainshteinc41c5932018-10-18 13:54:22 +0300544* [JFrog Artifactory](https://jfrog.com/artifactory/): Commercial offering. Support for Go 1.11 modules started with release 5.11 as described [here](https://jfrog.com/blog/goproxy-artifactory-go-registries/) and [here](https://www.jfrog.com/confluence/display/RTF/Go+Registry). From Artifactory version 6.2.0, please use [JFrog CLI 1.20.2](https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory#CLIforJFrogArtifactory-BuildingGoPackages) and above.
thepudds122bf9b2018-09-07 02:04:45 -0400545
546Note that you are not required to run a proxy. Rather, the go tooling in 1.11 has added optional proxy support via [GOPROXY](https://tip.golang.org/cmd/go/#hdr-Module_proxy_protocol) to enable more enterprise use cases (such as greater control), and also to better handle situations such as "GitHub is down" or people deleting GitHub repositories.
547
548### Can I control when go.mod gets updated and when the go tools use the network to satisfy dependencies?
thepudds00c21b72018-09-07 01:00:11 -0400549
550By default, a command like `go build` will reach out to the network as needed to satisfy imports.
551
552Some teams will want to disallow the go tooling from touching the network at certain points, or will want greater control regarding when the go tooling updates `go.mod`, how dependencies are obtained, and how vendoring is used.
553
554The go tooling provides a fair amount of flexibility to adjust or disable these default behaviors, including via `-mod=readonly`, `-mod=vendor`, `GOFLAGS`, `GOPROXY=off`, `GOPROXY=file:///filesystem/path`, `go mod vendor`, and `go mod download`.
555
556The details on these options are spread throughout the official documentation. One community attempt at a consolidated overview of knobs related to these behaviors is [here](https://github.com/thepudds/go-module-knobs/blob/master/README.md), which includes links to the official documentation for more information.
557
558### How do I use modules with CI systems such as Travis or CircleCI?
559
560The simplest approach is likely just setting the environment variable `GO111MODULE=on`, which should work with most CI systems.
561
562However, it can be valuable to run tests in CI on Go 1.11 with modules enabled as well as disabled, given some of your users will not have yet opted in to modules themselves. Vendoring is also a topic to consider.
563
564The following two blog posts cover these topics more concretely:
565
566* ["Using Go modules with vendor support on Travis CI"](https://arslan.io/2018/08/26/using-go-modules-with-vendor-support-on-travis-ci/) by Fatih Arslan
567* ["Go Modules and CircleCI"](https://medium.com/@toddkeech/go-modules-and-circleci-c0d6fac0b000) by Todd Keech
568
thepudds7afc40d2018-09-24 22:06:51 -0400569## FAQs — go.mod and go.sum
570
thepudds58207da2018-09-17 09:57:11 -0400571### Why does 'go mod tidy' record indirect and test dependencies in my 'go.mod'?
thepudds00c21b72018-09-07 01:00:11 -0400572
thepuddse392e8e2018-09-29 10:22:51 -0400573The modules system records precise dependency requirements in your `go.mod`. (For more details, see the [go.mod concepts](https://github.com/golang/go/wiki/Modules#gomod) section above or the [go.mod tip documentation](https://tip.golang.org/cmd/go/#hdr-The_go_mod_file)).
thepudds00c21b72018-09-07 01:00:11 -0400574
thepuddse392e8e2018-09-29 10:22:51 -0400575`go mod tidy` updates your current `go.mod` to include the dependencies needed for tests in your module — if a test fails, we must know which dependencies were used in order to reproduce the failure.
thepudds00c21b72018-09-07 01:00:11 -0400576
thepudds58207da2018-09-17 09:57:11 -0400577`go mod tidy` also ensures your current `go.mod` reflects the dependency requirements for all possible combinations of OS, architecture, and build tags (as described [here](https://github.com/golang/go/issues/25971#issuecomment-399091682)). In contrast, other commands like `go build` and `go test` only update `go.mod` to provide the packages imported by the requested packages under the current `GOOS`, `GOARCH`, and build tags (which is one reason `go mod tidy` might add requirements that were not added by `go build` or similar).
thepudds00c21b72018-09-07 01:00:11 -0400578
thepudds23256d12018-09-29 10:31:34 -0400579If a dependency of your module does not itself have a `go.mod` (e.g., because the dependency has not yet opted in to modules itself), or if its `go.mod` file is missing one or more of its dependencies (e.g., because the module author did not run `go mod tidy`), then the missing transitive dependencies will be added to _your_ module's requirements, along with an `// indirect` comment to indicate that the dependency is not from a direct import within your module.
580
581Note that this also means that any missing test dependencies from your direct or indirect dependencies will also be recorded in your `go.mod`. (An example of when this is important: `go test all` runs the tests of _all_ direct and indirect dependencies of your module, which is one way to validate that your current combination of versions work together. If a test fails in one of your dependencies when you run `go test all`, it is important to have a complete set of test dependency information recorded so that you have reproducible `go test all` behavior).
thepuddse392e8e2018-09-29 10:22:51 -0400582
thepuddsdced9eb2018-11-29 06:37:22 -0500583Another reason you might have `// indirect` dependencies in your `go.mod` file is if you have upgraded (or downgraded) one of your indirect dependencies beyond what is required by your direct dependencies, such as if you ran `go get -u` or `go get foo@1.2.3`. The go tooling needs a place to record those new versions, and it does so in your `go.mod` file (and it does not reach down into your dependencies to modify _their_ `go.mod` files).
584
585In general, the behaviors described above are part of how modules provide 100% reproducible builds and tests by recording precise dependency information.
thepudds00c21b72018-09-07 01:00:11 -0400586
Matthew Calea913f0e2018-10-23 15:58:35 -0500587If you are curious as to why a particular module is showing up in your `go.mod`, you can run `go mod why -m <module>` to [answer](https://tip.golang.org/cmd/go/#hdr-Explain_why_packages_or_modules_are_needed) that question. Other useful tools for inspecting requirements and versions include `go mod graph` and `go list -m all`.
thepudds00c21b72018-09-07 01:00:11 -0400588
thepudds58207da2018-09-17 09:57:11 -0400589### Is 'go.sum' a lock file? Why does 'go.sum' include information for module versions I am no longer using?
590
thepuddse4ddd272018-10-09 10:16:32 -0400591No, `go.sum` is not a lock file. For validation purposes, `go.sum` contains the expected cryptographic checksums of the content of specific module versions. See the [FAQ below](https://github.com/golang/go/wiki/Modules#should-i-commit-my-gosum-file-as-well-as-my-gomod-file) for more details on `go.sum` (including why you typically should check in `go.sum`) as well as the ["Module downloading and verification"](https://tip.golang.org/cmd/go/#hdr-Module_downloading_and_verification) section in the tip documentation.
thepudds58207da2018-09-17 09:57:11 -0400592
593In part because `go.sum` is not a lock file, it retains cryptographic checksums for module versions even after you stop using a module or particular module version. This allows validation of the checksums if you later resume using something, which provides additional safety.
594
thepuddsd86f5aa2018-09-17 10:21:31 -0400595In addition, your module's `go.sum` records checksums for all direct and indirect dependencies used in a build (and hence your `go.sum` will frequently have more modules listed than your `go.mod`).
596
thepuddse4ddd272018-10-09 10:16:32 -0400597### Should I still add a 'go.mod' file if I do not have any dependencies?
thepuddsd86f5aa2018-09-17 10:21:31 -0400598
599Yes. This supports working outside of GOPATH, helps communicate to the ecosystem that you are opting in to modules, and in addition the `module` directive in your `go.mod` serves as a definitive declaration of the identify of your code (which is one reason why import comments might eventually be deprecated). Of course, modules are purely an opt-in capability in Go 1.11.
600
thepuddse4ddd272018-10-09 10:16:32 -0400601### Should I commit my 'go.sum' file as well as my 'go.mod' file?
Paul Jollyb64be362018-10-08 12:15:43 +0100602
thepuddse4ddd272018-10-09 10:16:32 -0400603Typically your module's `go.sum` file should be committed along with your `go.mod` file.
Paul Jollyb64be362018-10-08 12:15:43 +0100604
thepuddse4ddd272018-10-09 10:16:32 -0400605* `go.sum` contains the expected cryptographic checksums of the content of specific module versions.
606* If someone clones your repository and downloads your dependencies using the go command, they will receive an error if there is any mismatch between their downloaded copies of your dependencies and the corresponding entries in your `go.sum`.
607* In addition, `go mod verify` checks that the on-disk cached copies of module downloads still match the entries in `go.sum`.
608* Note that `go.sum` is not a lock file as used in some alternative dependency management systems. (`go.mod` provides enough information for reproducible builds).
609* See very brief [rationale here](https://twitter.com/FiloSottile/status/1029404663358087173) from Filippo Valsorda on why you should check in your `go.sum`. See the ["Module downloading and verification"](https://tip.golang.org/cmd/go/#hdr-Module_downloading_and_verification) section of the tip documentation for more details. See possible future extensions being discussed for example in [#24117](https://github.com/golang/go/issues/24117) and [#25530](https://github.com/golang/go/issues/25530).
Paul Jollyb12f08b2018-10-08 12:18:21 +0100610
thepudds7afc40d2018-09-24 22:06:51 -0400611## FAQs — Semantic Import Versioning
612
613### Why must major version numbers appear in import paths?
614
615Please see the discussion on the semantic import versioning and the import compatibility rule in the ["Semantic Import Versioning"](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) concepts section above. See also the [blog post announcing the proposal](https://blog.golang.org/versioning-proposal), which talks more about the motivation and justification for the import compatibility rule.
616
617### Why are major versions v0, v1 omitted from import paths?"
618
619Please see the question "Why are major versions v0, v1 omitted from import paths?" in the earlier [FAQ from the official proposal discussion](https://github.com/golang/go/issues/24301#issuecomment-371228664).
620
thepudds856618c2018-09-07 00:20:35 -0400621### What are some implications of tagging my project with major version v0, v1, or making breaking changes with v2+?
thepudds1da29f62018-08-05 11:19:52 -0400622
thepudds856618c2018-09-07 00:20:35 -0400623In response to a comment about *"k8s does minor releases but changes the Go API in each minor release"*, Russ Cox made the following [response](https://github.com/kubernetes/kubernetes/pull/65683#issuecomment-403705882) that highlights some implications for picking v0, v1, vs. frequently making breaking changes with v2, v3, v4, etc. with your project:
thepudds1da29f62018-08-05 11:19:52 -0400624
625> I don't fully understand the k8s dev cycle etc, but I think generally the k8s team needs to decide/confirm what they intend to guarantee to users about stability and then apply version numbers accordingly to express that.
626>
627> * To make a promise about API compatibility (which seems like the best user experience!) then start doing that and use 1.X.Y.
628> * To have the flexibility to make backwards-incompatible changes in every release but allow different parts of a large program to upgrade their code on different schedules, meaning different parts can use different major versions of the API in one program, then use X.Y.0, along with import paths like k8s.io/client/vX/foo.
629> * To make no promises about API compatible and also require every build to have only one copy of the k8s libraries no matter what, with the implied forcing of all parts of a build to use the same version even if not all of them are ready for it, then use 0.X.Y.
630
thepudds162ccfe2018-08-05 11:27:29 -0400631On a related note, Kubernetes has some atypical build approaches (currently including custom wrapper scripts on top of godep), and hence Kubernetes is an imperfect example for many other projects, but it will likely be an interesting example as [Kubernetes moves towards adopting Go 1.11 modules](https://github.com/kubernetes/kubernetes/pull/64731#issuecomment-407345841).
632
thepudds7afc40d2018-09-24 22:06:51 -0400633## FAQs — Minimal Version Selection
634
635### Won't minimal version selection keep developers from getting important updates?
636
637Please see the question "Won't minimal version selection keep developers from getting important updates?" in the earlier [FAQ from the official proposal discussion](https://github.com/golang/go/issues/24301#issuecomment-371228664).
638
639## FAQs — Possible Problems
640
thepudds856618c2018-09-07 00:20:35 -0400641### Why does `go build` require gcc, and why are prebuilt packages such as net/http not used?
642
643In short:
644
645> Because the pre-built packages are non-module builds and can’t be reused. Sorry. Disable cgo for now or install gcc.
646
647This is only an issue when opting in to modules (e.g., via `GO111MODULE=on`). See [#26988](https://github.com/golang/go/issues/26988#issuecomment-417886417) for additional discussion.
648
649### Do modules work with relative imports like `import "./subdir"`?
650
651No. See [#26645](https://github.com/golang/go/issues/26645#issuecomment-408572701), which includes:
652
653> In modules, there finally is a name for the subdirectory. If the parent directory says "module m" then the subdirectory is imported as "m/subdir", no longer "./subdir".
654
Sylvain Laurent5b4c4e02018-10-03 17:02:28 +0200655### Some needed files may not be present in populated vendor directory
656
657Only *.go files are copied inside vendor directory by `go mod vendor`, this is by design, see [#27618](https://github.com/golang/go/issues/27618).
658
thepudds7afc40d2018-09-24 22:06:51 -0400659## FAQs — Miscellaneous
660
Dmitry Savintsev660f1e32018-10-12 13:49:21 +0200661### How did the `go mod` commands change in `go1.11beta3` and `go1.11` release?
thepudds84465052018-09-07 01:09:12 -0400662
Dmitry Savintsev7df33ca2018-10-12 13:55:43 +0200663In go1.11beta3 and go1.11, there was a significant change for the `go mod` commands. Older material and blogs might still use the older commands from before the change. See the [release documentation](https://golang.org/cmd/go/#hdr-Module_maintenance) and `go mod help` output for the up-to-date information:
thepudds84465052018-09-07 01:09:12 -0400664```
Dmitry Savintsev660f1e32018-10-12 13:49:21 +0200665$ go mod help
666Go mod provides access to operations on modules.
thepudds84465052018-09-07 01:09:12 -0400667
Dmitry Savintsev660f1e32018-10-12 13:49:21 +0200668Note that support for modules is built into all the go commands,
669not just 'go mod'. For example, day-to-day adding, removing, upgrading,
670and downgrading of dependencies should be done using 'go get'.
671See 'go help modules' for an overview of module functionality.
672
673Usage:
674
675 go mod <command> [arguments]
676
677The commands are:
678
679 download download modules to local cache
680 edit edit go.mod from tools or scripts
681 graph print module requirement graph
682 init initialize new module in current directory
683 tidy add missing and remove unused modules
684 vendor make vendored copy of dependencies
685 verify verify dependencies have expected content
686 why explain why packages or modules are needed
687
688Use "go help mod <command>" for more information about a command.
thepudds84465052018-09-07 01:09:12 -0400689```
Dmitry Savintsev660f1e32018-10-12 13:49:21 +0200690
691For a historical reference, take a look at the [CL](https://go-review.googlesource.com/c/go/+/126655) briefly covering the rationale and the list of new vs. old commands (though further changed by 1.11 release):
thepudds84465052018-09-07 01:09:12 -0400692```
thepudds84465052018-09-07 01:09:12 -0400693Splitting out the individual commands makes both the docs
694and the implementations dramatically easier to read.
695It simplifies the command lines
696(go mod -init -module m is now 'go mod init m')
697and allows command-specific flags.
698```
699
thepuddse01f40e2018-08-01 01:05:48 -0400700