Skip to content

Instantly share code, notes, and snippets.

@chu-yik
Last active November 12, 2021 01:11
Show Gist options
  • Save chu-yik/62fada55ed442390b0bf3b9e776b0457 to your computer and use it in GitHub Desktop.
Save chu-yik/62fada55ed442390b0bf3b9e776b0457 to your computer and use it in GitHub Desktop.
Go modules takeaways

Readings

Takeaways

  • package (no go.mod) != module (has go.mod)

--

  • to init module
go mod init example.com/hello
# creates `go.mod`

--

  • to download dependencies
    • and to remove unused dependencies
go mod tidy
  • now both direct and indirect (// indirect) dependencies are in go.mod?

--

  • to see dependencies
go list -m all
# filter?
go list -m rsc.io/q...
  • to list available version
go list -m -versions rsc.io/sampler

--

  • to upgrade module
# latest (default is @latest)
go get golang.org/x/text 

# specific version
go get rsc.io/sampler@v1.3.1
  • to upgrade dependencies of module
go get -u rsc.io/quote/v3
# go get: upgraded rsc.io/sampler v1.3.1 => v1.99.99

From go help get:

The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.

--

  • the v3 in rsc.io/quote/v3 represents a different major version (not backward compatible)

The go command allows a build to include at most one version of any particular module path, meaning at most one of each major version: one rsc.io/quote, one rsc.io/quote/v2, one rsc.io/quote/v3, and so on.

This gives module authors a clear rule about possible duplication of a single module path: it is impossible for a program to build with both rsc.io/quote v1.5.2 and rsc.io/quote v1.6.0.

--

  • to understand why a package is used
go mod why -m rsc.io/quote/v3

or

go mod graph | grep rsc.io/quote/v3

--

Pseudo-versions

Reading

Pseudo-versions are a special type of pre-release version. Pseudo-versions are useful when a user needs to depend on a project that has not published any semantic version tags, or develop against a commit that hasn’t been tagged yet, but users should not assume that pseudo-versions provide a stable or well-tested API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment