Go.mod: Difference between revisions
Line 50: | Line 50: | ||
==Variations of <tt>go get</tt> Usage== | ==Variations of <tt>go get</tt> Usage== | ||
<font color=darkkhaki>TODO: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them</font> | <font color=darkkhaki> | ||
TODO: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them | |||
Deplete: [[Go Commands - get|get]] | |||
<code>go get</code> is sensitive to the Git configuration present in <code>~/.gitconfig</code>, especially repository configuration: | |||
<font size=-2> | |||
[url "git@github.example.com:some-repo"] | |||
insteadOf = https://github.example.com/some-repo | |||
</font> | |||
</font> |
Revision as of 18:06, 14 December 2023
External
Internal
Overview
go.mod
declares the module path, which is the import path prefix for all packages within the module. It also tracks the modules that provide dependencies to this module. go.mod
is located in the root of the module, which is also the root of the source tree for the module.
One way to create it is with go mod init
:
go mod init <module-path>
go.mod
stays with the code, including in the source repository.
As go
tools are used to manage dependencies, the tools update the go.mod
file so that it maintains the current list of dependencies. when a dependency is added, go
creates a go.sum
file that contains checksums of dependency modules. This is used to verify the integrity of the downloaded module files.
Example
module example.com/hello
go 1.21.0
require rsc.io/quote v1.5.2
require (
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
rsc.io/sampler v1.3.0 // indirect
)
Initialize go.mod
In the module root directory, initialize the go.mod
file.
go mod init <module-path>
go mod init example.com/generic
Adding a Dependency to a Module
Dependencies are added with the go get
command. The command updates go.mod
by adding the require
directive for dependencies. The require
directive tracks the minimum version of a module that the module depends on. The command also updates the module source code and places in the module cache.
go get github.com/apache/yunikorn-core@v1.4.0
Note that you are adding the dependency in advance without actually referring it from the module source code, go get
will add an "// indirect" comment for that specific dependency in go.mod
.
Variations of go get Usage
TODO: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them
Deplete: get
go get
is sensitive to the Git configuration present in ~/.gitconfig
, especially repository configuration:
[url "git@github.example.com:some-repo"] insteadOf = https://github.example.com/some-repo