Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 1 | # Go 1.11 Modules |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 2 | |
thepudds | 15f1244 | 2018-08-25 22:01:50 -0400 | [diff] [blame] | 3 | Go 1.11 includes preliminary support for versioned modules as proposed [here](https://golang.org/design/24301-versioned-go). |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 4 | |
thepudds | 49436e5 | 2018-08-25 22:00:24 -0400 | [diff] [blame] | 5 | 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`. |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 6 | |
thepudds | a0c342a | 2018-09-02 09:23:03 -0400 | [diff] [blame] | 7 | The 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. |
thepudds | 634f341 | 2018-07-28 15:27:09 -0400 | [diff] [blame] | 8 | |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 9 | ## Table of Contents |
| 10 | |
thepudds | e4474f2 | 2018-08-02 22:26:17 -0400 | [diff] [blame] | 11 | The remaining content on this page is organized as follows: |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 12 | * [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) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 14 | * [Modules](https://github.com/golang/go/wiki/Modules#modules) |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 15 | * [go.mod](https://github.com/golang/go/wiki/Modules#gomod) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 16 | * [Version Selection](https://github.com/golang/go/wiki/Modules#version-selection) |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 17 | * [Semantic Import Versioning](https://github.com/golang/go/wiki/Modules#semantic-import-versioning) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 18 | * [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) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 25 | * [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) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 29 | * [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) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 30 | * [What community tooling exists for working with modules?](https://github.com/golang/go/wiki/Modules#what-community-tooling-exists-for-working-with-modules) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 31 | * [When should I use the 'replace' directive?](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 32 | * [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) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 34 | * [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) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 36 | * [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) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 37 | * [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) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 38 | * [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) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 40 | * [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) |
thepudds | f445693 | 2018-09-07 01:51:55 -0400 | [diff] [blame] | 41 | * [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) |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 42 | * [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) |
thepudds | fc0364b | 2018-08-02 22:10:15 -0400 | [diff] [blame] | 43 | |
thepudds | 847a5de | 2018-07-27 07:54:08 -0400 | [diff] [blame] | 44 | ## Installing and Activating Module Support |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 45 | |
thepudds | 847a5de | 2018-07-27 07:54:08 -0400 | [diff] [blame] | 46 | To use modules, you currently have three install options: |
thepudds | 49436e5 | 2018-08-25 22:00:24 -0400 | [diff] [blame] | 47 | * [Install the latest Go 1.11 release](https://golang.org/dl/). |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 48 | * [Install the Go toolchain from source](https://golang.org/doc/install/source) on the `master` branch. |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 49 | * Install the `vgo` binary from the [`vgo` subrepository](https://github.com/golang/vgo) (and replace `go` with `vgo` in the commands below). |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 50 | |
thepudds | eef37be | 2018-07-27 08:35:11 -0400 | [diff] [blame] | 51 | Once installed, you can then activate module support in one of three ways: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 52 | * 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. |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 54 | * Invoke the binary named `vgo` (if you have installed `vgo` from the subrepository). |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 55 | |
| 56 | ## New Concepts |
| 57 | |
thepudds | 345f562 | 2018-09-06 08:55:00 -0400 | [diff] [blame] | 58 | These 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). |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 59 | |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 60 | ### Modules |
| 61 | |
Baruch Sadogursky | 43df113 | 2018-08-28 07:18:04 -0600 | [diff] [blame] | 62 | A *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. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 63 | |
thepudds | def8ca7 | 2018-08-28 17:26:39 -0400 | [diff] [blame] | 64 | Modules record precise dependency requirements and create reproducible builds. |
thepudds | cc84ca2 | 2018-08-28 17:24:14 -0400 | [diff] [blame] | 65 | |
thepudds | def8ca7 | 2018-08-28 17:26:39 -0400 | [diff] [blame] | 66 | Modules 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)). |
thepudds | 80d05d3 | 2018-08-13 07:38:09 -0400 | [diff] [blame] | 67 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 68 | ### go.mod |
| 69 | |
thepudds | def8ca7 | 2018-08-28 17:26:39 -0400 | [diff] [blame] | 70 | A 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. |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 71 | |
thepudds | 8ff4544 | 2018-08-26 12:45:52 -0400 | [diff] [blame] | 72 | All 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`. |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 73 | |
thepudds | e508c05 | 2018-08-15 12:57:00 -0400 | [diff] [blame] | 74 | Module files may include comments and will look familiar to a Go programmer. Here is an example `go.mod` file: |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 75 | |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 76 | ``` |
thepudds | 72779d8 | 2018-08-15 12:48:53 -0400 | [diff] [blame] | 77 | module github.com/my/module/v3 |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 78 | |
| 79 | require ( |
thepudds | 72779d8 | 2018-08-15 12:48:53 -0400 | [diff] [blame] | 80 | github.com/some/dependency v1.2.3 |
| 81 | github.com/another/dependency v0.1.0 |
thepudds | 8ff4544 | 2018-08-26 12:45:52 -0400 | [diff] [blame] | 82 | github.com/additional/dependency/v4 v4.0.0 |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 83 | ) |
| 84 | ``` |
| 85 | |
thepudds | c53293c | 2018-08-15 13:05:09 -0400 | [diff] [blame] | 86 | There are four directives: `module`, `require`, `exclude`, `replace`. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 87 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 88 | `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. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 89 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 90 | In Go source code, packages are imported using the full path including the module, for example: |
thepudds | e508c05 | 2018-08-15 12:57:00 -0400 | [diff] [blame] | 91 | * `import "example.com/my/module/v2/pkg/foo"` to import `foo` from the v2 version of module `example.com/my/module`. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 92 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 93 | ### Version Selection |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 94 | |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 95 | If 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). |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 96 | |
thepudds | 7806bc7 | 2018-08-29 00:16:41 -0400 | [diff] [blame] | 97 | The *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. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 98 | |
thepudds | 738d95c | 2018-08-29 08:37:13 -0400 | [diff] [blame] | 99 | As 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. |
thepudds | c164d5a | 2018-08-03 13:04:28 -0400 | [diff] [blame] | 100 | |
thepudds | 91ffcea | 2018-08-02 23:55:11 -0400 | [diff] [blame] | 101 | For 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). |
thepudds | 5077edf | 2018-08-02 09:01:12 -0400 | [diff] [blame] | 102 | |
thepudds | 6ae9d6f | 2018-08-15 15:52:57 -0400 | [diff] [blame] | 103 | To see a list of the selected module versions (including indirect dependencies), use `go list -m all`. |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 104 | |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 105 | See 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. |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 106 | |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 107 | ### Semantic Import Versioning |
| 108 | |
thepudds | 01c7434 | 2018-08-25 10:49:21 -0400 | [diff] [blame] | 109 | For 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 | |
| 113 | The 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 | |
| 118 | Recall [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 | |
| 120 | For code opting in to Go modules, the net result is the following rules: |
thepudds | 23d2952 | 2018-08-25 13:00:46 -0400 | [diff] [blame] | 121 | * Follow [semantic versioning](https://semver.org/) (with tags such as `v1.2.3`). |
thepudds | 656e835 | 2018-08-21 15:32:45 -0400 | [diff] [blame] | 122 | * 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 | |
thepudds | 01c7434 | 2018-08-25 10:49:21 -0400 | [diff] [blame] | 125 | In 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. |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 126 | |
thepudds | aa43ea4 | 2018-08-15 11:52:47 -0400 | [diff] [blame] | 127 | See 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 | |
thepudds | ab3a7ed | 2018-09-05 09:14:13 -0400 | [diff] [blame] | 129 | This 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). |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 130 | |
| 131 | There 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: |
| 132 | 1. 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. |
thepudds | d0ef5ad | 2018-08-15 12:07:14 -0400 | [diff] [blame] | 134 | * 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. |
thepudds | aa43ea4 | 2018-08-15 11:52:47 -0400 | [diff] [blame] | 135 | 2. 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. |
thepudds | 0e8012e | 2018-08-05 12:24:11 -0400 | [diff] [blame] | 137 | |
thepudds | 656e835 | 2018-08-21 15:32:45 -0400 | [diff] [blame] | 138 | The 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. |
thepudds | 1433190 | 2018-08-05 12:09:17 -0400 | [diff] [blame] | 139 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 140 | ## How to Define a Module |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 141 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 142 | To create a `go.mod` for an existing project: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 143 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 144 | 1. Navigate to the root of the module's source tree and activate module mode in the `go` command: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 145 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 146 | ``` |
| 147 | $ cd $GOPATH/src/<project path> |
| 148 | $ export GO111MODULE=on |
| 149 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 150 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 151 | or: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 152 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 153 | ``` |
| 154 | $ cd <project path outside $GOPATH/src> |
| 155 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 156 | |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 157 | 2. Create the initial module definition and write it to the `go.mod` file: |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 158 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 159 | ``` |
thepudds | 49436e5 | 2018-08-25 22:00:24 -0400 | [diff] [blame] | 160 | # if using Go 1.11, 'master', or the latest 'vgo', use the newer form: |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame] | 161 | $ go mod init |
thepudds | f105431 | 2018-08-01 01:30:34 -0400 | [diff] [blame] | 162 | |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame] | 163 | # in go1.11beta2 and earlier, the older form was: |
| 164 | $ go mod -init |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 165 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 166 | |
thepudds | 9bdb272 | 2018-07-27 07:02:48 -0400 | [diff] [blame] | 167 | 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. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 168 | |
thepudds | 8d39bc3 | 2018-08-13 13:34:49 -0400 | [diff] [blame] | 169 | 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. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 170 | |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 171 | ``` |
thepudds | 49436e5 | 2018-08-25 22:00:24 -0400 | [diff] [blame] | 172 | # if using Go 1.11, 'master', or the latest 'vgo', use the newer form: |
thepudds | f105431 | 2018-08-01 01:30:34 -0400 | [diff] [blame] | 173 | $ go mod init example.com/my/module/v2 |
thepudds | 6f434ef | 2018-08-13 13:29:51 -0400 | [diff] [blame] | 174 | |
| 175 | # in go1.11beta2 and earlier, the older form was: |
| 176 | $ go mod -init -module example.com/my/module/v2 |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 177 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 178 | |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 179 | 3. Build the module. This will automatically add missing or unconverted dependencies as needed to satisfy imports for this particular build invocation: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 180 | |
| 181 | ``` |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 182 | $ go build ./... |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 183 | ``` |
thepudds | 6f42754 | 2018-07-31 13:33:16 -0400 | [diff] [blame] | 184 | 4. Test the module as configured to ensure that it works with the selected versions: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 185 | |
| 186 | ``` |
| 187 | $ go test ./... |
| 188 | ``` |
| 189 | |
thepudds | d5e66ee | 2018-08-15 13:20:27 -0400 | [diff] [blame] | 190 | 5. (Optional) Run the tests for your module plus the tests for all direct and indirect dependencies to check for incompatibilities: |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 191 | |
| 192 | ``` |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 193 | $ go test all |
Bryan C. Mills | 0d7c8e0 | 2018-07-16 15:50:05 -0400 | [diff] [blame] | 194 | ``` |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 195 | |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 196 | Prior 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. |
thepudds | 74241a1 | 2018-07-31 13:31:00 -0400 | [diff] [blame] | 197 | |
thepudds | 432bda3 | 2018-08-07 05:22:33 -0400 | [diff] [blame] | 198 | For 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 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 200 | ## How to Upgrade and Downgrade Dependencies |
Bryan C. Mills | 75f8173 | 2018-07-16 14:15:15 -0400 | [diff] [blame] | 201 | |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 202 | Day-to-day adding, removing, upgrading, and downgrading of dependencies should be done using 'go get', which will automatically update the `go.mod` file. |
| 203 | |
thepudds | 61c12ce | 2018-07-27 08:04:34 -0400 | [diff] [blame] | 204 | In 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). |
thepudds | b05a844 | 2018-07-26 08:18:42 -0400 | [diff] [blame] | 205 | |
thepudds | 927647d | 2018-07-26 09:03:39 -0400 | [diff] [blame] | 206 | To upgrade to the latest version for all transitive dependencies of the current module: |
thepudds | 5af74db | 2018-08-01 18:14:36 -0400 | [diff] [blame] | 207 | * run `go get -u` to use the latest *minor or patch* releases |
| 208 | * run `go get -u=patch` to use the latest *patch* releases |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 209 | |
thepudds | 8f75c6c | 2018-08-01 09:18:37 -0400 | [diff] [blame] | 210 | To 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 | |
thepudds | ff8f0f8 | 2018-08-25 13:07:59 -0400 | [diff] [blame] | 212 | `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 | |
| 214 | In 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. |
thepudds | 72779d8 | 2018-08-15 12:48:53 -0400 | [diff] [blame] | 215 | |
thepudds | 90d81be | 2018-08-26 12:21:40 -0400 | [diff] [blame] | 216 | Modules 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 | |
thepudds | 8f75c6c | 2018-08-01 09:18:37 -0400 | [diff] [blame] | 218 | See 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. |
thepudds | b05a844 | 2018-07-26 08:18:42 -0400 | [diff] [blame] | 219 | |
thepudds | d5e66ee | 2018-08-15 13:20:27 -0400 | [diff] [blame] | 220 | After 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: |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 221 | ``` |
thepudds | 4b3d893 | 2018-07-27 06:50:53 -0400 | [diff] [blame] | 222 | $ go test all |
thepudds | ee879c5 | 2018-07-25 12:08:34 -0400 | [diff] [blame] | 223 | ``` |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 224 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 225 | ## How to Prepare for a Release |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 226 | |
thepudds | df13b28 | 2018-08-01 00:38:40 -0400 | [diff] [blame] | 227 | Best 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). |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 228 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 229 | Some current suggested best practices to consider prior to tagging a release: |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 230 | |
thepudds | cb4e8b5 | 2018-08-28 15:01:45 -0400 | [diff] [blame] | 231 | * 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. |
thepudds | f918d5c | 2018-07-31 13:36:22 -0400 | [diff] [blame] | 233 | |
thepudds | d8674ab | 2018-08-15 15:22:10 -0400 | [diff] [blame] | 234 | * 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. |
thepudds | d5e66ee | 2018-08-15 13:20:27 -0400 | [diff] [blame] | 235 | * 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. |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 236 | * 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. |
thepudds | 61aba5c | 2018-07-27 07:10:40 -0400 | [diff] [blame] | 237 | |
thepudds | abb30ff | 2018-08-15 09:32:02 -0400 | [diff] [blame] | 238 | * 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. |
thepudds | 3d101f0 | 2018-08-15 14:19:10 -0400 | [diff] [blame] | 240 | * 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`. |
thepudds | 2b5afbd | 2018-08-29 00:35:43 -0400 | [diff] [blame] | 242 | * 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). |
thepudds | b98d7cf | 2018-08-15 09:39:44 -0400 | [diff] [blame] | 243 | * 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). |
thepudds | abb30ff | 2018-08-15 09:32:02 -0400 | [diff] [blame] | 244 | |
thepudds | d0ef5ad | 2018-08-15 12:07:14 -0400 | [diff] [blame] | 245 | If 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 | |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 247 | ## Additional Resources |
thepudds | f398d51 | 2018-07-22 18:10:33 -0400 | [diff] [blame] | 248 | |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 249 | ### Documentation and Proposal |
| 250 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 251 | * 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. |
thepudds | 592f743 | 2018-07-26 10:03:47 -0400 | [diff] [blame] | 257 | * 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 |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 260 | * Official [Versioned Go Modules Proposal](https://golang.org/design/24301-versioned-go) (last updated March 20, 2018) |
| 261 | |
| 262 | ### Introductory Material |
| 263 | |
thepudds | 345f562 | 2018-09-06 08:55:00 -0400 | [diff] [blame] | 264 | * 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) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 265 | * 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) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 266 | * 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) |
thepudds | 345f562 | 2018-09-06 08:55:00 -0400 | [diff] [blame] | 267 | * 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) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 268 | * 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 | |
thepudds | 345f562 | 2018-09-06 08:55:00 -0400 | [diff] [blame] | 272 | * 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) |
thepudds | 592f743 | 2018-07-26 10:03:47 -0400 | [diff] [blame] | 274 | * Blog post ["The vgo proposal is accepted. Now what?"](https://research.swtch.com/vgo-accepted) by Russ Cox (May 29, 2018) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 275 | * Includes summary of what it means that versioned modules are currently an experimental opt-in feature |
thepudds | 345f562 | 2018-09-06 08:55:00 -0400 | [diff] [blame] | 276 | * 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) |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 277 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 278 | ## Changes Since the Initial Vgo Proposal |
| 279 | |
thepudds | a7d3054 | 2018-08-03 18:02:38 -0400 | [diff] [blame] | 280 | As 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 | |
| 282 | Here is a partial list of some of the larger changes and improvements, almost all of which were primarily based on feedback from the community: |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 283 | |
| 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)) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 285 | * 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)) |
thepudds | 42f1dea | 2018-08-03 18:07:15 -0400 | [diff] [blame] | 286 | * 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)) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 287 | * 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)) |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 288 | * 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)) |
thepudds | 55989cc | 2018-08-05 20:39:02 -0400 | [diff] [blame] | 289 | * 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) |
thepudds | 0168d01 | 2018-08-02 22:24:15 -0400 | [diff] [blame] | 290 | * 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)) |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 293 | * 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)) |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 294 | * **Most likely**: better support for installing specific versions of programs to GOBIN ([#24250](https://github.com/golang/go/issues/24250#issuecomment-377553022)) |
| 295 | |
thepudds | 8891964 | 2018-08-02 21:51:04 -0400 | [diff] [blame] | 296 | ## GitHub Issues |
thepudds | 8390d03 | 2018-07-22 23:37:00 -0400 | [diff] [blame] | 297 | |
thepudds | df13b28 | 2018-08-01 00:38:40 -0400 | [diff] [blame] | 298 | * [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) |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 301 | * 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 | |
thepudds | d944259 | 2018-08-02 09:44:49 -0400 | [diff] [blame] | 307 | The `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). |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 308 | |
thepudds | 601d231 | 2018-08-06 23:26:24 -0400 | [diff] [blame] | 309 | One 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: |
thepudds | 0bb0f8d | 2018-08-01 01:51:19 -0400 | [diff] [blame] | 310 | * declaring deprecated versions as [described](https://research.swtch.com/vgo-module) in the initial `vgo` blog series |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 311 | * 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 |
thepudds | 00c21b7 | 2018-09-07 01:00:11 -0400 | [diff] [blame] | 312 | * 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) |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 313 | |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 314 | ### When do I get old behavior vs. new module-based behavior? |
| 315 | |
| 316 | In general, modules are opt-in for Go 1.11, so by design old behavior is preserved by default. |
| 317 | |
| 318 | Summarizing 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) |
thepudds | 038b77e | 2018-08-28 12:20:18 -0400 | [diff] [blame] | 321 | * Outside GOPATH while inside a file tree with a `go.mod` — defaults to modules behavior |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 322 | * GO111MODULE environment variable: |
thepudds | e24c771 | 2018-08-25 15:09:57 -0400 | [diff] [blame] | 323 | * 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 |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 326 | |
thepudds | 320b624 | 2018-09-06 23:52:46 -0400 | [diff] [blame] | 327 | ### Why does installing a tool via `go get` fail with error `cannot find main module`? |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 328 | |
thepudds | 868cbbc | 2018-09-06 23:59:14 -0400 | [diff] [blame] | 329 | This occurs when you have set `GO111MODULE=on`, but are not inside of a file tree with a `go.mod` when you run `go get`. |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 330 | |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 331 | The simplest solution is to leave `GO111MODULE` unset (or equivalently explicitly set to `GO111MODULE=auto`), which avoids this error. |
thepudds | e24c771 | 2018-08-25 15:09:57 -0400 | [diff] [blame] | 332 | |
thepudds | 868cbbc | 2018-09-06 23:59:14 -0400 | [diff] [blame] | 333 | Recall 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. |
thepudds | e24c771 | 2018-08-25 15:09:57 -0400 | [diff] [blame] | 334 | |
thepudds | 868cbbc | 2018-09-06 23:59:14 -0400 | [diff] [blame] | 335 | Solution alternatives include: |
thepudds | 586c5c0 | 2018-08-25 21:37:33 -0400 | [diff] [blame] | 336 | |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 337 | 1. 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`. |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 338 | |
thepudds | 320b624 | 2018-09-06 23:52:46 -0400 | [diff] [blame] | 339 | 2. 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 | |
| 341 | 3. 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 | |
| 343 | 4. 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 | |
| 345 | 5. 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 | |
| 347 | This 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 | |
| 351 | This FAQ has been discussing tracking _globally_ installed tools. |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 352 | |
thepudds | 93bae8f | 2018-08-25 22:39:46 -0400 | [diff] [blame] | 353 | If instead you want to track the tools required by a _specific_ module, see the next FAQ. |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 354 | |
| 355 | ### How can I track tool dependencies for a module? |
| 356 | |
| 357 | If 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 | |
| 361 | then 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 | |
| 363 | The 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. |
thepudds | e24c771 | 2018-08-25 15:09:57 -0400 | [diff] [blame] | 366 | > |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 367 | > I like it because it does not introduce new mechanisms. |
thepudds | e24c771 | 2018-08-25 15:09:57 -0400 | [diff] [blame] | 368 | > |
thepudds | 758e608 | 2018-08-25 15:02:30 -0400 | [diff] [blame] | 369 | > It simply reuses existing ones. |
| 370 | |
thepudds | b488c3a | 2018-09-02 12:26:20 -0400 | [diff] [blame] | 371 | ### What is the status of module support in IDEs, editors and standard tools like goimports, gorename, etc? |
| 372 | |
| 373 | Support for modules is starting to land in editors and IDEs. |
| 374 | |
| 375 | For 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 | |
| 382 | The 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 | |
| 384 | Some 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 | |
| 389 | In 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 | |
thepudds | 636e98d | 2018-09-07 01:38:02 -0400 | [diff] [blame] | 391 | The 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 | |
| 395 | The 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. |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 420 | * [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. |
thepudds | 636e98d | 2018-09-07 01:38:02 -0400 | [diff] [blame] | 421 | |
| 422 | ### Can I work entirely outside of VCS on my local filesystem? |
| 423 | |
| 424 | Yes. VCS is not required. |
| 425 | |
| 426 | This 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 | |
| 428 | If 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 | ``` |
| 431 | module example.com/me/hello |
| 432 | |
| 433 | require ( |
| 434 | example.com/me/goodbye v0.0.0 |
| 435 | ) |
| 436 | |
| 437 | replace example.com/me/goodbye => ../goodbye |
| 438 | ``` |
| 439 | As 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 | |
| 441 | A small runnable example is shown in this [thread](https://groups.google.com/d/msg/golang-nuts/1nYoAMFZVVM/eppaRW2rCAAJ). |
thepudds | b488c3a | 2018-09-02 12:26:20 -0400 | [diff] [blame] | 442 | |
thepudds | 00c21b7 | 2018-09-07 01:00:11 -0400 | [diff] [blame] | 443 | ### How do I use vendoring with modules? Is vendoring going away? |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 444 | |
thepudds | 00c21b7 | 2018-09-07 01:00:11 -0400 | [diff] [blame] | 445 | The 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 | |
| 447 | In 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 | |
| 453 | Older 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 | |
| 455 | If 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 | |
thepudds | 122bf9b | 2018-09-07 02:04:45 -0400 | [diff] [blame^] | 457 | ### Are there "always on" module repositories and enterprise proxies? |
| 458 | |
| 459 | Publicly hosted "always on" immutable module repositories and optional privately hosted proxies and repositories are becoming available. |
| 460 | |
| 461 | For 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 | |
| 465 | Note 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? |
thepudds | 00c21b7 | 2018-09-07 01:00:11 -0400 | [diff] [blame] | 468 | |
| 469 | By default, a command like `go build` will reach out to the network as needed to satisfy imports. |
| 470 | |
| 471 | Some 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 | |
| 473 | The 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 | |
| 475 | The 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 | |
| 479 | The simplest approach is likely just setting the environment variable `GO111MODULE=on`, which should work with most CI systems. |
| 480 | |
| 481 | However, 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 | |
| 483 | The 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 | |
| 492 | In 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 | |
| 496 | If 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 | |
| 498 | If 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 | |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 500 | ### What are some implications of tagging my project with major version v0, v1, or making breaking changes with v2+? |
thepudds | 1da29f6 | 2018-08-05 11:19:52 -0400 | [diff] [blame] | 501 | |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 502 | In 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: |
thepudds | 1da29f6 | 2018-08-05 11:19:52 -0400 | [diff] [blame] | 503 | |
| 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 | |
thepudds | 162ccfe | 2018-08-05 11:27:29 -0400 | [diff] [blame] | 510 | On 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 | |
thepudds | 856618c | 2018-09-07 00:20:35 -0400 | [diff] [blame] | 512 | ### Should I still add a go.mod file if I do not have any dependencies? |
| 513 | |
| 514 | Yes. 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 | |
| 518 | In 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 | |
| 522 | This 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 | |
| 526 | No. 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 | |
thepudds | 8446505 | 2018-09-07 01:09:12 -0400 | [diff] [blame] | 530 | ### How did the `go mod` commands change in `go1.11beta3`? |
| 531 | |
| 532 | In 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 | ``` |
| 534 | The current "go mod" command does too many things. |
| 535 | |
| 536 | It looks like "everything you might want to do with modules" |
| 537 | which causes people to think all module operations go through |
| 538 | "go mod", which is the opposite of the seamless integration we're |
| 539 | going for. In particular too many people think "go mod -require" |
| 540 | and "go get" are the same. |
| 541 | ``` |
| 542 | and: |
| 543 | ``` |
| 544 | split "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 | |
| 554 | Splitting out the individual commands makes both the docs |
| 555 | and the implementations dramatically easier to read. |
| 556 | It simplifies the command lines |
| 557 | (go mod -init -module m is now 'go mod init m') |
| 558 | and allows command-specific flags. |
| 559 | ``` |
| 560 | |
thepudds | fd489a1 | 2018-08-01 02:47:49 -0400 | [diff] [blame] | 561 | ### Additional frequently asked questions |
thepudds | e01f40e | 2018-08-01 01:05:48 -0400 | [diff] [blame] | 562 | |
| 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?" |