Module: A module is a collection of Go package stored in a file tree with a go.mod
file at its root. The go.mod
file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build.
Creating a module
go mod init <PATH>
: creates a new module, initializing the go.mod
file that describes it.
1 | go mod init example.com/hello |
Adding a dependency
The primary motivation for Go modules was to improve the experience of using (that is, adding a dependency on) code written by other developers.
The go command resolves imports by using the specific dependency module versions listed in go.mod
. When it encounters an import of a package not provided by any module in go.mod
, the go command automatically looks up the module containing that package and adds it to go.mod
, using the latest version.
Your module now literally depends on the new dependency in critical areas such as correctness, security, and proper licensing, just to name a few.
go list -m all
: lists the current module and all its dependencies.
In addition to go.mod
, the go command maintains a file named go.sum
containing the expected cryptographic hashes of the content of specific module versions:
1 | cat go.sum |
Both go.mod
and go.sum
should be checked into version control.
Upgrade dependency
TBA