blob: 4f7b8e65e6e81db1f6f62121f13dd5835970d9b7 [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
thepudds15f12442018-08-25 22:01:50 -04003Go 1.11 includes preliminary support for versioned modules as proposed [here](https://golang.org/design/24301-versioned-go).
Bryan C. Mills75f81732018-07-16 14:15:15 -04004
thepudds49436e52018-08-25 22:00:24 -04005Go 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`.
thepuddsf398d512018-07-22 18:10:33 -04006
thepuddsa0c342a2018-09-02 09:23:03 -04007The 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 Go 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.
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:
thepuddsfc0364b2018-08-02 22:10:15 -040012* [Installing and Activating Module Support](https://github.com/golang/go/wiki/Modules#installing-and-activating-module-support)
13* [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)
thepuddsfc0364b2018-08-02 22:10:15 -040018* [How to Define a Module](https://github.com/golang/go/wiki/Modules#how-to-define-a-module)
19* [How to Upgrade and Downgrade Dependencies](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies)
20* [How to Prepare for a Release](https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release)
21* [Additional Resources](https://github.com/golang/go/wiki/Modules#additional-resources)
22* [Changes Since the Initial Vgo Proposal](https://github.com/golang/go/wiki/Modules#changes-since-the-initial-vgo-proposal)
23* [GitHub Issues](https://github.com/golang/go/wiki/Modules#github-issues)
24* [FAQ](https://github.com/golang/go/wiki/Modules#faq)
thepuddsf4456932018-09-07 01:51:55 -040025 * [How are versions marked as incompatible?](https://github.com/golang/go/wiki/Modules#how-are-versions-marked-as-incompatible)
26 * [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)
27 * [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)
28 * [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 -040029 * [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)
thepuddsf4456932018-09-07 01:51:55 -040030 * [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 -040031 * [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 -040032 * [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)
33 * [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 -040034 * [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)
35 * [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 -040036 * [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)
thepudds122bf9b2018-09-07 02:04:45 -040037 * [Why does 'go mod tidy' put so many indirect dependencies in my go.mod?](https://github.com/golang/go/wiki/Modules#why-does-go-mod-tidy-put-so-many-indirect-dependencies-in-my-gomod)
thepuddsf4456932018-09-07 01:51:55 -040038 * [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)
39 * [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)
thepudds122bf9b2018-09-07 02:04:45 -040040 * [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 -040041 * [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)
thepudds122bf9b2018-09-07 02:04:45 -040042 * [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 -040043
thepudds847a5de2018-07-27 07:54:08 -040044## Installing and Activating Module Support
Bryan C. Mills75f81732018-07-16 14:15:15 -040045
thepudds847a5de2018-07-27 07:54:08 -040046To use modules, you currently have three install options:
thepudds49436e52018-08-25 22:00:24 -040047* [Install the latest Go 1.11 release](https://golang.org/dl/).
thepuddsfd489a12018-08-01 02:47:49 -040048* [Install the Go toolchain from source](https://golang.org/doc/install/source) on the `master` branch.
thepuddsfd489a12018-08-01 02:47:49 -040049* Install the `vgo` binary from the [`vgo` subrepository](https://github.com/golang/vgo) (and replace `go` with `vgo` in the commands below).
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -040050
thepuddseef37be2018-07-27 08:35:11 -040051Once installed, you can then activate module support in one of three ways:
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -040052* 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`).
53* Invoke the `go` command with `GO111MODULE=on` in the command environment.
thepuddsfd489a12018-08-01 02:47:49 -040054* Invoke the binary named `vgo` (if you have installed `vgo` from the subrepository).
Bryan C. Mills75f81732018-07-16 14:15:15 -040055
56## New Concepts
57
thepudds345f5622018-09-06 08:55:00 -040058These 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 -040059
Bryan C. Mills75f81732018-07-16 14:15:15 -040060### Modules
61
Baruch Sadogursky43df1132018-08-28 07:18:04 -060062A *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 -040063
thepuddsdef8ca72018-08-28 17:26:39 -040064Modules record precise dependency requirements and create reproducible builds.
thepuddscc84ca22018-08-28 17:24:14 -040065
thepuddsdef8ca72018-08-28 17:26:39 -040066Modules 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 -040067
thepudds0e8012e2018-08-05 12:24:11 -040068### go.mod
69
thepuddsdef8ca72018-08-28 17:26:39 -040070A 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 -040071
thepudds8ff45442018-08-26 12:45:52 -040072All 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/thing/foo` and `example.com/my/thing/bar`, the first line in your `go.mod` file typically would be `module example.com/my/thing`.
thepudds0e8012e2018-08-05 12:24:11 -040073
thepuddse508c052018-08-15 12:57:00 -040074Module files may include comments and will look familiar to a Go programmer. Here is an example `go.mod` file:
thepudds0e8012e2018-08-05 12:24:11 -040075
Bryan C. Mills75f81732018-07-16 14:15:15 -040076```
thepudds72779d82018-08-15 12:48:53 -040077module github.com/my/module/v3
Bryan C. Mills75f81732018-07-16 14:15:15 -040078
79require (
thepudds72779d82018-08-15 12:48:53 -040080 github.com/some/dependency v1.2.3
81 github.com/another/dependency v0.1.0
thepudds8ff45442018-08-26 12:45:52 -040082 github.com/additional/dependency/v4 v4.0.0
Bryan C. Mills75f81732018-07-16 14:15:15 -040083)
84```
85
thepuddsc53293c2018-08-15 13:05:09 -040086There are four directives: `module`, `require`, `exclude`, `replace`.
Bryan C. Mills75f81732018-07-16 14:15:15 -040087
thepudds0e8012e2018-08-05 12:24:11 -040088`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. (TODO: show example exclude and replace directives and/or FAQ on when to use them).
Bryan C. Mills75f81732018-07-16 14:15:15 -040089
thepudds0e8012e2018-08-05 12:24:11 -040090In Go source code, packages are imported using the full path including the module, for example:
thepuddse508c052018-08-15 12:57:00 -040091 * `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 -040092
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -040093### Version Selection
Bryan C. Mills75f81732018-07-16 14:15:15 -040094
thepudds0e8012e2018-08-05 12:24:11 -040095If 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 -040096
thepudds7806bc72018-08-29 00:16:41 -040097The *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 -040098
thepudds738d95c2018-08-29 08:37:13 -040099As 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 -0400100
thepudds91ffcea2018-08-02 23:55:11 -0400101For 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 -0400102
thepudds6ae9d6f2018-08-15 15:52:57 -0400103To see a list of the selected module versions (including indirect dependencies), use `go list -m all`.
Bryan C. Mills75f81732018-07-16 14:15:15 -0400104
thepudds0168d012018-08-02 22:24:15 -0400105See 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 -0400106
thepudds14331902018-08-05 12:09:17 -0400107### Semantic Import Versioning
108
thepudds01c74342018-08-25 10:49:21 -0400109For many years, the official Go FAQ has included this advice on package versioning:
110
111> "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."
112
113The last sentence is especially important. With Go 1.11 modules, that advice is formalized into the _import compatibility rule_:
114
115> "If an old package and a new package have the same import path,
116> the new package must be backwards compatible with the old package."
117
118Recall [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_.
119
120For code opting in to Go modules, the net result is the following rules:
thepudds23d29522018-08-25 13:00:46 -0400121* Follow [semantic versioning](https://semver.org/) (with tags such as `v1.2.3`).
thepudds656e8352018-08-21 15:32:45 -0400122* 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"`).
123* If the module is version v0 or v1, do _not_ include the major version in either the module path or the import path.
124
thepudds01c74342018-08-25 10:49:21 -0400125In general, packages with different import paths (e.g., due to different major versions) are different packages. 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 -0400126
thepuddsaa43ea42018-08-15 11:52:47 -0400127See 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.
128
thepuddsab3a7ed2018-09-05 09:14:13 -0400129This 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_ yet 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 -0400130
131There are two ways to release a v2 or higher module version. Using the example of creating a `v2.0.0` release, the two options are:
1321. Update the `go.mod` file to include a `/v2` at the end of the module path. Tag the release with `v2.0.0`.
133 * To avoid confusion with this approach, consider putting the `v2.*.*` commits on a separate v2 branch.
thepuddsd0ef5ad2018-08-15 12:07:14 -0400134 * If instead you have been previously releasing on master and would prefer to tag `v2.0.0` on master, that is a viable option, but consider creating a v1 branch for any future v1 bug fixes.
thepuddsaa43ea42018-08-15 11:52:47 -04001352. Alternatively, create a new `v2` subdirectory (e.g., `my/module/v2`) and place a new `go.mod` file in that subdirectory. The module path must end with `/v2`. Copy or move the code into the `v2` subdirectory. Tag the release with `v2.0.0`.
136 * 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 or higher module created using this approach.
thepudds0e8012e2018-08-05 12:24:11 -0400137
thepudds656e8352018-08-21 15:32:45 -0400138The behavior of how modules interact with existing pre-module packages with v2+ tags has evolved over the prototype and beta processes; an important related recent change was [issue 26238](https://golang.org/issue/26238), which substantially [improved the go command behavior](https://github.com/golang/go/issues/25967#issuecomment-407567904) for existing packages with v2+ tags.
thepudds14331902018-08-05 12:09:17 -0400139
thepudds88919642018-08-02 21:51:04 -0400140## How to Define a Module
Bryan C. Mills75f81732018-07-16 14:15:15 -0400141
thepuddsfd489a12018-08-01 02:47:49 -0400142To create a `go.mod` for an existing project:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400143
thepudds6f427542018-07-31 13:33:16 -04001441. Navigate to the root of the module's source tree and activate module mode in the `go` command:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400145
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400146 ```
147 $ cd $GOPATH/src/<project path>
148 $ export GO111MODULE=on
149 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400150
thepudds6f427542018-07-31 13:33:16 -0400151 or:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400152
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400153 ```
154 $ cd <project path outside $GOPATH/src>
155 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400156
thepudds6f427542018-07-31 13:33:16 -04001572. Create the initial module definition and write it to the `go.mod` file:
Bryan C. Mills75f81732018-07-16 14:15:15 -0400158
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400159 ```
thepudds49436e52018-08-25 22:00:24 -0400160 # if using Go 1.11, 'master', or the latest 'vgo', use the newer form:
thepudds6f434ef2018-08-13 13:29:51 -0400161 $ go mod init
thepuddsf1054312018-08-01 01:30:34 -0400162
thepudds6f434ef2018-08-13 13:29:51 -0400163 # in go1.11beta2 and earlier, the older form was:
164 $ go mod -init
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400165 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400166
thepudds9bdb2722018-07-27 07:02:48 -0400167 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 -0400168
thepudds8d39bc32018-08-13 13:34:49 -0400169 If `go mod` cannot automatically determine an appropriate module path (e.g., if running outside of VCS), 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 -0400170
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400171 ```
thepudds49436e52018-08-25 22:00:24 -0400172 # if using Go 1.11, 'master', or the latest 'vgo', use the newer form:
thepuddsf1054312018-08-01 01:30:34 -0400173 $ go mod init example.com/my/module/v2
thepudds6f434ef2018-08-13 13:29:51 -0400174
175 # in go1.11beta2 and earlier, the older form was:
176 $ go mod -init -module example.com/my/module/v2
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400177 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400178
thepudds74241a12018-07-31 13:31:00 -04001793. 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 -0400180
181 ```
thepudds74241a12018-07-31 13:31:00 -0400182 $ go build ./...
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400183 ```
thepudds6f427542018-07-31 13:33:16 -04001844. Test the module as configured to ensure that it works with the selected versions:
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400185
186 ```
187 $ go test ./...
188 ```
189
thepuddsd5e66ee2018-08-15 13:20:27 -04001905. (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 -0400191
192 ```
thepudds4b3d8932018-07-27 06:50:53 -0400193 $ go test all
Bryan C. Mills0d7c8e02018-07-16 15:50:05 -0400194 ```
Bryan C. Mills75f81732018-07-16 14:15:15 -0400195
thepudds0168d012018-08-02 22:24:15 -0400196Prior 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 -0400197
thepudds432bda32018-08-07 05:22:33 -0400198For 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).
199
thepudds88919642018-08-02 21:51:04 -0400200## How to Upgrade and Downgrade Dependencies
Bryan C. Mills75f81732018-07-16 14:15:15 -0400201
thepudds4b3d8932018-07-27 06:50:53 -0400202Day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get', which will automatically update the `go.mod` file.
203
thepudds61c12ce2018-07-27 08:04:34 -0400204In 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 -0400205
thepudds927647d2018-07-26 09:03:39 -0400206To upgrade to the latest version for all transitive dependencies of the current module:
thepudds5af74db2018-08-01 18:14:36 -0400207 * run `go get -u` to use the latest *minor or patch* releases
208 * run `go get -u=patch` to use the latest *patch* releases
thepuddsee879c52018-07-25 12:08:34 -0400209
thepudds8f75c6c2018-08-01 09:18:37 -0400210To 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'`.
211
thepuddsff8f0f82018-08-25 13:07:59 -0400212`go get github.com/gorilla/mux` obtains the latest version with a [semver](https://semver.org/) tag. Using 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.
213
214In 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 -0400215
thepudds90d81be2018-08-26 12:21:40 -0400216Modules 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`).
217
thepudds8f75c6c2018-08-01 09:18:37 -0400218See 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 -0400219
thepuddsd5e66ee2018-08-15 13:20:27 -0400220After 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:
thepuddsee879c52018-07-25 12:08:34 -0400221 ```
thepudds4b3d8932018-07-27 06:50:53 -0400222 $ go test all
thepuddsee879c52018-07-25 12:08:34 -0400223 ```
thepuddsf398d512018-07-22 18:10:33 -0400224
thepudds88919642018-08-02 21:51:04 -0400225## How to Prepare for a Release
thepudds61aba5c2018-07-27 07:10:40 -0400226
thepuddsdf13b282018-08-01 00:38:40 -0400227Best 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 -0400228
thepudds88919642018-08-02 21:51:04 -0400229Some current suggested best practices to consider prior to tagging a release:
thepudds61aba5c2018-07-27 07:10:40 -0400230
thepuddscb4e8b52018-08-28 15:01:45 -0400231* Run `go mod tidy` (or if running go1.11beta2 or earlier: `go mod -sync`) 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)).
232 * 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 -0400233
thepuddsd8674ab2018-08-15 15:22:10 -0400234* 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 -0400235 * 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 -0400236 * 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 -0400237
thepuddsabb30ff2018-08-15 09:32:02 -0400238* Typically the `go.sum` file should be checked in along with the `go.mod` file.
239 * `go.sum` contains the expected cryptographic checksums of the content of specific module versions.
thepudds3d101f02018-08-15 14:19:10 -0400240 * 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`.
241 * In addition, `go mod verify` checks that the on-disk cached copies of module downloads still match the entries in `go.sum`.
thepudds2b5afbd2018-08-29 00:35:43 -0400242 * 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).
thepuddsb98d7cf2018-08-15 09:39:44 -0400243 * 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 very brief [rationale here](https://twitter.com/FiloSottile/status/1029404663358087173). 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).
thepuddsabb30ff2018-08-15 09:32:02 -0400244
thepuddsd0ef5ad2018-08-15 12:07:14 -0400245If you are releasing a v2 or higher module, please also see the related considerations in the ["Semantic Import Versioning" section](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) above.
246
thepudds8390d032018-07-22 23:37:00 -0400247## Additional Resources
thepuddsf398d512018-07-22 18:10:33 -0400248
thepudds8390d032018-07-22 23:37:00 -0400249### Documentation and Proposal
250
thepuddsfd489a12018-08-01 02:47:49 -0400251* Official documentation:
252 * Latest [HTML documentation for modules on tip.golang.org](https://tip.golang.org/cmd/go/#hdr-Modules__module_versions__and_more)
253 * Run `go help modules` for more about modules. (This is the main entry point for modules topics via `go help`)
254 * Run `go help mod` for more about the `go mod` command.
255 * Run `go help module-get` for more about the behavior of `go get` when in module-aware mode.
256 * 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 -0400257* The initial ["Go & Versioning"](https://research.swtch.com/vgo) series of blog posts on `vgo` by Russ Cox (first posted February 20, 2018)
258* Official [golang.org blog post introducing the proposal](https://blog.golang.org/versioning-proposal) (March 26, 2018)
259 * 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 -0400260* Official [Versioned Go Modules Proposal](https://golang.org/design/24301-versioned-go) (last updated March 20, 2018)
261
262### Introductory Material
263
thepudds345f5622018-09-06 08:55:00 -0400264* Example-focused 35-minute introductory talk ["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 -0400265* 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 -0400266* 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)
thepudds345f5622018-09-06 08:55:00 -0400267* 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 -0400268 * Succinctly covers the philosophy behind the design of versioned Go modules, including the three core principles of "Compatibility", "Repeatability", and "Cooperation"
269
270### Additional Material
271
thepudds345f5622018-09-06 08:55:00 -0400272* 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)
273* 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 -0400274* 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 -0400275 * Includes summary of what it means that versioned modules are currently an experimental opt-in feature
thepudds345f5622018-09-06 08:55:00 -0400276* 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 -0400277
thepudds88919642018-08-02 21:51:04 -0400278## Changes Since the Initial Vgo Proposal
279
thepuddsa7d30542018-08-03 18:02:38 -0400280As part of the proposal, prototype, and beta processes, there have been over 400 issues created by the overall community. Please continue to supply feedback.
281
282Here 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 -0400283
284* 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 -0400285* 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 -0400286* 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 -0400287* 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 -0400288* 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 -0400289* 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 -0400290* Added more flexible replace directives ([CL](https://go-review.googlesource.com/c/vgo/+/122400))
291* Added additional ways to interrogate modules (for human consumption, as well as for better editor / IDE integration)
292* 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 -0400293* 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 -0400294* **Most likely**: better support for installing specific versions of programs to GOBIN ([#24250](https://github.com/golang/go/issues/24250#issuecomment-377553022))
295
thepudds88919642018-08-02 21:51:04 -0400296## GitHub Issues
thepudds8390d032018-07-22 23:37:00 -0400297
thepuddsdf13b282018-08-01 00:38:40 -0400298* [Currently open module issues](https://golang.org/issues?q=is%3Aopen+is%3Aissue+label:modules)
299* [Closed module issues](https://golang.org/issues?q=is%3Aclosed+is%3Aissue+label:modules)
300* [Closed vgo issues](https://github.com/golang/go/issues?page=3&q=-label%3Amodules+vgo+is%3Aclosed)
thepuddse01f40e2018-08-01 01:05:48 -0400301* 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
302
303## FAQ
304
305### How are versions marked as incompatible?
306
thepuddsd9442592018-08-02 09:44:49 -0400307The `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 -0400308
thepudds601d2312018-08-06 23:26:24 -0400309One 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 -0400310* declaring deprecated versions as [described](https://research.swtch.com/vgo-module) in the initial `vgo` blog series
thepuddse01f40e2018-08-01 01:05:48 -0400311* 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 -0400312* 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 -0400313
thepudds758e6082018-08-25 15:02:30 -0400314### When do I get old behavior vs. new module-based behavior?
315
316In general, modules are opt-in for Go 1.11, so by design old behavior is preserved by default.
317
318Summarizing when you get the old 1.10 status quo behavior vs. the new opt-in modules-based behavior:
319
320* Inside GOPATH — defaults to old 1.10 behavior (ignoring modules)
thepudds038b77e2018-08-28 12:20:18 -0400321* Outside GOPATH while inside a file tree with a `go.mod` — defaults to modules behavior
thepudds758e6082018-08-25 15:02:30 -0400322* GO111MODULE environment variable:
thepuddse24c7712018-08-25 15:09:57 -0400323 * unset or `auto` — default behavior above
324 * `on` — force module support on regardless of directory location
325 * `off` — force module support off regardless of directory location
thepudds758e6082018-08-25 15:02:30 -0400326
thepudds320b6242018-09-06 23:52:46 -0400327### Why does installing a tool via `go get` fail with error `cannot find main module`?
thepudds758e6082018-08-25 15:02:30 -0400328
thepudds868cbbc2018-09-06 23:59:14 -0400329This 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 -0400330
thepudds856618c2018-09-07 00:20:35 -0400331The simplest solution is to leave `GO111MODULE` unset (or equivalently explicitly set to `GO111MODULE=auto`), which avoids this error.
thepuddse24c7712018-08-25 15:09:57 -0400332
thepudds868cbbc2018-09-06 23:59:14 -0400333Recall 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 -0400334
thepudds868cbbc2018-09-06 23:59:14 -0400335Solution alternatives include:
thepudds586c5c02018-08-25 21:37:33 -0400336
thepudds856618c2018-09-07 00:20:35 -04003371. 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 -0400338
thepudds320b6242018-09-06 23:52:46 -04003392. 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'`
340
3413. 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]`).
342
3434. 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.
344
3455. 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.
346
347This 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:
348
349> 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.
350
351This FAQ has been discussing tracking _globally_ installed tools.
thepudds758e6082018-08-25 15:02:30 -0400352
thepudds93bae8f2018-08-25 22:39:46 -0400353If instead you want to track the tools required by a _specific_ module, see the next FAQ.
thepudds758e6082018-08-25 15:02:30 -0400354
355### How can I track tool dependencies for a module?
356
357If you:
358 * want to use a go-based tool (e.g. stringer) while working on a module, and
359 * 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
360
361then 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).
362
363The brief rationale (also from #25922):
364
365> 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 -0400366>
thepudds758e6082018-08-25 15:02:30 -0400367> I like it because it does not introduce new mechanisms.
thepuddse24c7712018-08-25 15:09:57 -0400368>
thepudds758e6082018-08-25 15:02:30 -0400369> It simply reuses existing ones.
370
thepuddsb488c3a2018-09-02 12:26:20 -0400371### What is the status of module support in IDEs, editors and standard tools like goimports, gorename, etc?
372
373Support for modules is starting to land in editors and IDEs.
374
375For example:
376* **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/).
377* **VS Code**: work is in progress and looking for contribtors to help. Tracking issue is [#1532](https://github.com/Microsoft/vscode-go/issues/1532).
378* **Atom with go-plus**: tracking issue is [#761](https://github.com/joefitzgerald/go-plus/issues/761).
379* **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).
380* **emacs with go-mode.el**: tracking issue in [#237](https://github.com/dominikh/go-mode.el/issues/237).
381
382The 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.
383
384Some tracking issues for particular tools includes:
385* **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`.
386* **goimports**: possible work-in-progress approach in [CL 128362](https://go-review.googlesource.com/c/tools/+/128362).
387* **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).
388
389In 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).
390
thepudds636e98d2018-09-07 01:38:02 -0400391The 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`.
392
393### What community tooling exists for working with modules?
394
395The community is starting to build tooling on top of modules. For example:
396
397* [github.com/rogpeppe/gohack](https://github.com/rogpeppe/gohack)
398 * A new community tool to automate and greatly simplify `replace` and multi-module workflows, including allowing you to easily modify one of your dependencies
399 * For example, `gohack example.com/some/dependency` automatically clones the appropriate repository and adds the necessary `replace` directives to your `go.mod`
400 * Remove all gohack replace statements with `gohack -u`
401 * The project is continuing to expand to make other module-related workflows easier
402* [github.com/marwan-at-work/mod](https://github.com/marwan-at-work/mod)
403 * Command line tool to automatically upgrade/downgrade major versions for modules
404 * Automatically adjusts `go.mod` files and related import statements in go source code
405 * Helps with upgrades, or when first opting in to modules with a v2+ package
406* [github.com/goware/modvendor](https://github.com/goware/modvendor)
407 * Helps copy additional files into the `vendor/` folder, such as shell scripts, .cpp files, .proto files, etc.
408
409### When should I use the replace directive?
410
411* `replace` directives in go.mod 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.
412* 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 (without needing to update the import paths in the actual source code).
413* One sample use case is if you need to fix something in a dependency, you can have a local fork and add the something like the following in your top-level `go.mod`:
414 * `replace example.com/original/import/path => /your/forked/import/path`
415* `replace` also allows the top-level module control over the exact version used for a dependency, such as:
416 * `replace example.com/some/dependency => example.com/some/dependency@v1.2.3`
417* `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:
418 * `replace example.com/project/foo => ../foo`
419* 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 -0400420* [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 -0400421
422### Can I work entirely outside of VCS on my local filesystem?
423
424Yes. VCS is not required.
425
426This is very simple if you have a single module you want to edit at a time, and you can place the `go.mod` anywhere.
427
428If 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):
429
430```
431module example.com/me/hello
432
433require (
434 example.com/me/goodbye v0.0.0
435)
436
437replace example.com/me/goodbye => ../goodbye
438```
439As 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. (A common mistake is to include `replace foo => ../foo` without having a corresponding `require foo v0.0.0`).
440
441A small runnable example is shown in this [thread](https://groups.google.com/d/msg/golang-nuts/1nYoAMFZVVM/eppaRW2rCAAJ).
thepuddsb488c3a2018-09-02 12:26:20 -0400442
thepudds00c21b72018-09-07 01:00:11 -0400443### How do I use vendoring with modules? Is vendoring going away?
thepuddse01f40e2018-08-01 01:05:48 -0400444
thepudds00c21b72018-09-07 01:00:11 -0400445The 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.
446
447In brief, to use vendoring with modules:
448* `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.
449* By default, go commands like `go build` ignore the vendor directory when in module mode.
450* 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.
451* Some people will want to routinely opt-in to vendoring by setting a `GOFLAGS=-mod-vendor` environment variable.
452
453Older 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.
454
455If 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.
456
thepudds122bf9b2018-09-07 02:04:45 -0400457### Are there "always on" module repositories and enterprise proxies?
458
459Publicly hosted "always on" immutable module repositories and optional privately hosted proxies and repositories are becoming available.
460
461For example:
462* [Project Athens](https://github.com/gomods/athens): Open source project in the works and looking for contributors.
463* [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).
464
465Note 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.
466
467### 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 -0400468
469By default, a command like `go build` will reach out to the network as needed to satisfy imports.
470
471Some 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.
472
473The 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`.
474
475The 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.
476
477### How do I use modules with CI systems such as Travis or CircleCI?
478
479The simplest approach is likely just setting the environment variable `GO111MODULE=on`, which should work with most CI systems.
480
481However, 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.
482
483The following two blog posts cover these topics more concretely:
484
485* ["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
486* ["Go Modules and CircleCI"](https://medium.com/@toddkeech/go-modules-and-circleci-c0d6fac0b000) by Todd Keech
487
488### Why does `go mod tidy` put so many indirect dependencies in my `go.mod`?
489
490`go mod tidy` ensures your current go.mod reflects all possible build tags/OS/architecture combinations (as described [here](https://github.com/golang/go/issues/25971#issuecomment-399091682)).
491
492In contrast, other commands like `go build` and `go test` only update `go.mod` based on the current build invocation's tags/OS/architecture.
493
494`go mod tidy` can also pull in test dependencies that `go build` might not. Recording dependency information for tests helps provide reproducible tests for you, and also for a consumer of your module who might run your tests via `go test all` or similar.
495
496If a particular dependency of your module does not itself have a `go.mod` (because the dependency has not yet opted in to modules itself), then your dependency lacking a `go.mod` will have _its_ dependencies recorded in a parent `go.mod` (such as your `go.mod`), along with an `// indirect` comment to indicate that the recorded information is not from a direct dependency of your module.
497
498If you are curious why a particular module is showing up in your `go.mod`, then running `go mod why -m <module>` is one way 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`.
499
thepudds856618c2018-09-07 00:20:35 -0400500### 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 -0400501
thepudds856618c2018-09-07 00:20:35 -0400502In 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 -0400503
504> 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.
505>
506> * To make a promise about API compatibility (which seems like the best user experience!) then start doing that and use 1.X.Y.
507> * 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.
508> * 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.
509
thepudds162ccfe2018-08-05 11:27:29 -0400510On 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).
511
thepudds856618c2018-09-07 00:20:35 -0400512### Should I still add a go.mod file if I do not have any dependencies?
513
514Yes. This helps communicate to the ecosystem that you are opting in to modules, supports working outside of GOPATH, and in addition the `module` directive in your `go.mod` serves as a definitive declaration of the identify of your code (which is part of the reason the older approach of import comments might eventually be deprecated). That said, modules overall are an opt-in capability in Go 1.11.
515
516### Why does `go build` require gcc, and why are prebuilt packages such as net/http not used?
517
518In short:
519
520> Because the pre-built packages are non-module builds and can’t be reused. Sorry. Disable cgo for now or install gcc.
521
522This 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.
523
524### Do modules work with relative imports like `import "./subdir"`?
525
526No. See [#26645](https://github.com/golang/go/issues/26645#issuecomment-408572701), which includes:
527
528> 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".
529
thepudds84465052018-09-07 01:09:12 -0400530### How did the `go mod` commands change in `go1.11beta3`?
531
532In go1.11beta3, 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 [tip documentation](https://tip.golang.org/cmd/go/#hdr-Module_maintenance) as well as two snippets from the [CL](https://go-review.googlesource.com/c/go/+/126655) briefly covering the rationale and the list of new vs. old commands:
533```
534The current "go mod" command does too many things.
535
536It looks like "everything you might want to do with modules"
537which causes people to think all module operations go through
538"go mod", which is the opposite of the seamless integration we're
539going for. In particular too many people think "go mod -require"
540and "go get" are the same.
541```
542and:
543```
544split "go mod" into multiple subcommands:
545
546 go mod edit # old go mod -require ...
547 go mod fix # old go mod -fix
548 go mod graph # old go mod -graph
549 go mod init # old go mod -init
550 go mod tidy # old go mod -sync
551 go mod vendor # old go mod -vendor
552 go mod verify # old go mod -verify
553
554Splitting out the individual commands makes both the docs
555and the implementations dramatically easier to read.
556It simplifies the command lines
557(go mod -init -module m is now 'go mod init m')
558and allows command-specific flags.
559```
560
thepuddsfd489a12018-08-01 02:47:49 -0400561### Additional frequently asked questions
thepuddse01f40e2018-08-01 01:05:48 -0400562
563* Please see the earlier [FAQ from the official proposal discussion](https://github.com/golang/go/issues/24301#issuecomment-371228664), including answers to common question such as:
564 * "Won't minimal version selection keep developers from getting important updates?"
565 * "Why are major versions v0, v1 omitted from import paths?"
566 * "Why must major version numbers appear in import paths?"