Go.mod: Difference between revisions
Line 71: | Line 71: | ||
Normally, <code>go get</code> is used with published, versioned releases of a project's dependencies. However, there are situations that require experimentally modifying a dependency to use it with the current project. The Go documentation refers to this situation as "developing and testing against unpublished module code". There are two ways to access a modified dependency: in a [[#Using_Local_Module_Code|local directory]] on the development machine and in a [[#Using_an_Arbitrary_Fork_Module_Code|forked repository]]. | Normally, <code>go get</code> is used with published, versioned releases of a project's dependencies. However, there are situations that require experimentally modifying a dependency to use it with the current project. The Go documentation refers to this situation as "developing and testing against unpublished module code". There are two ways to access a modified dependency: in a [[#Using_Local_Module_Code|local directory]] on the development machine and in a [[#Using_an_Arbitrary_Fork_Module_Code|forked repository]]. | ||
==Using Local Module Code== | ==Using Local Module Code== | ||
A very common situation when experimenting with the source of a dependency is to clone the dependency repository locally, and modify the code. It is convenient, and possible, to express the <code>go.mod</code> dependency to point to the local directory. | |||
The <code>go.mod</code> syntax to express this situation is: | |||
<syntaxhighlight lang='py'> | |||
... | |||
require example.com/somemodule v0.0.0-unpublished | |||
replace example.com/somemodule v0.0.0-unpublished => ../somemodule | |||
</syntaxhighlight> | |||
The configuration expresses the fact the dependency version we are using it is not published anywhere (<code>v0.0.0-unpublished</code>), and to get it the <code>replace</code> directive path (<code>=> ../somemodule</code) must be used. | |||
Note that <code>go.mod</code> you should avoid modifying <code>go.mod</code> manually. Use the following commands to generate the configuration: | |||
<syntaxhighlight lang='bash'> | |||
go mod edit -replace=example.com/somemodule@v0.0.0-unpublished=../somemodule | |||
</syntaxhighlight> | |||
then pull the source code in the [[Go_Modules#Module_Cache|module cache]] with: | |||
<syntaxhighlight lang='bash'> | |||
go get example.com/somemodule@v0.0.0-unpublished | |||
</syntaxhighlight> | |||
==Using an Arbitrary Fork Module Code== | ==Using an Arbitrary Fork Module Code== |
Revision as of 18:22, 4 January 2024
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>
The file can also be created manually with a text editor, but Go documentation recommends to make changes to the file via go
commands.
go.mod
stays with the code, including in the source repository.
A typical go.mod
file usually assumes that your project is hosted in GitHub, but this is not a requirement. The module path can be anything.
As go
tools are used to manage dependencies, the tools update the go.mod
file so that it maintains the current list of dependencies.
go.sum
When a dependency is added, go
creates a go.sum
file that contains checksums, in form of cryptographic hashes, of dependency modules. This is used to verify the integrity of the downloaded module files. This file should not edited manually.
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 and authenticate each module it downloads. This ensures that it's unchanged ged from when the module was published.
go get [-u] 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
.
TODO: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them
Variations of go get
Update Module's Dependencies
Use go list
to check if there are newer versions of dependencies. If there are available useful upgrades, use go get
command again to add the particular newer version.
Synchronize Dependencies
TODO: https://go.dev/doc/modules/managing-dependencies#synchronizing
Using Unpublished Module Code
Normally, go get
is used with published, versioned releases of a project's dependencies. However, there are situations that require experimentally modifying a dependency to use it with the current project. The Go documentation refers to this situation as "developing and testing against unpublished module code". There are two ways to access a modified dependency: in a local directory on the development machine and in a forked repository.
Using Local Module Code
A very common situation when experimenting with the source of a dependency is to clone the dependency repository locally, and modify the code. It is convenient, and possible, to express the go.mod
dependency to point to the local directory.
The go.mod
syntax to express this situation is:
...
require example.com/somemodule v0.0.0-unpublished
replace example.com/somemodule v0.0.0-unpublished => ../somemodule
The configuration expresses the fact the dependency version we are using it is not published anywhere (v0.0.0-unpublished
), and to get it the replace
directive path (=> ../somemodule</code) must be used.
Note that
go.mod
you should avoid modifying go.mod
manually. Use the following commands to generate the configuration:
go mod edit -replace=example.com/somemodule@v0.0.0-unpublished=../somemodule
then pull the source code in the module cache with:
go get example.com/somemodule@v0.0.0-unpublished
Using an Arbitrary Fork Module Code
TODO: https://go.dev/doc/modules/managing-dependencies#external_fork
Getting a Specific Commit
TODO: https://go.dev/doc/modules/managing-dependencies#repo_identifier