Go.mod: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 14: Line 14:
<code>go.mod</code> stays with the code, including in the source repository.
<code>go.mod</code> stays with the code, including in the source repository.
=Example=
=Example=
<syntaxhighlight lang='go'>
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
)
</syntaxhighlight>
<font color=darkkhaki>CONSOLIDATE with [[go.mod]].</font>
<code>go.mod</code> declares the [[#Module_Path|module path]], which is the [[Go_Packages#Import_Path|import path]] prefix for all [[Go_Packages#Overview|packages]] within the module. It also tracks the modules that provide dependencies to this module.
One way to create it is with <code>[[Go_Tool#mod|go mod init]]</code>:
<syntaxhighlight lang='bash'>
go mod init <module-path>
</syntaxhighlight>
<code>go.mod</code> stays with the code, including in the source repository.
==Example==
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
module example.com/hello
module example.com/hello

Revision as of 17:09, 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.

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.

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
)