Golang modules: Difference between revisions

From wikinotes
(Created page with "Go projects/packages are referred to as '''modules'''. = Modules = <blockquote> == Creating/Requiring Modules == <blockquote> See go.mod file. </blockquote><!-- Creating...")
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Go projects/packages are referred to as '''modules'''.
Go modules are a collection of packages that are intended to be released together.<br>
You define your project requirements in at the module level,<br>
and you can publish module-versions to the go package index.


= Modules =
= Documentation =
<blockquote>
<blockquote>
== Creating/Requiring Modules ==
{| class="wikitable"
|-
| private modules || https://go.dev/ref/mod#private-modules
|-
| module proxies || https://go.dev/ref/mod#module-proxy
|-
| module cache || https://go.dev/ref/mod#module-cache
|-
|}
</blockquote><!-- Documentation -->
 
= Locations =
<blockquote>
{| class="wikitable"
|-
| go module index || https://index.golang.org/index
|-
| go checksum index || https://sum.golang.org/
|-
|}
</blockquote><!-- Locations -->
 
= module path =
<blockquote>
<blockquote>
See [[go.mod file]].
The module path determines how your package is identified, <br>
what the executable will be called,<br>
how it will be downloaded,<br>
and it can be filtered to ensure it is private.
 
example:
<syntaxhighlight lang="bash">
example.com/x/foo
</syntaxhighlight>
 
See [[golang module path]].
</blockquote><!-- module path -->
 
= Go.mod file =
<blockquote>
The [[go.mod file]] stores metadata about your project, and it's requirements.<br>
There are several tools to help manage it, but this is the source of truth.


</blockquote><!-- Creating/Requiring Modules -->
</blockquote><!-- Creating/Requiring Modules -->
</blockquote><!-- Modules -->


= Imports =
= Module Requirements =
<blockquote>
== Tools ==
<blockquote>
See also [[go.mod file]] .
 
<syntaxhighlight lang="bash">
go mod init example.com/x/myproject  # create a new module
 
go mod graph  # show requirements tree
go mod tidy    # ensure go.mod matches src (add requirements if necessary)
go mod vendor  #
 
# add/update requirements to project
go get example.com/module@latest  # update to latest
go get example.com/module v1.1.1  # update/downgrade to target version
</syntaxhighlight>
</blockquote><!-- Module Requirements -->
 
== Vendoring ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
 
import (
</blockquote><!-- Vendoring -->
    "fmt"
</blockquote><!-- Module Requirements -->
    "golang.org/x/example/stringutil"
 
)
= Publishing Modules =
<blockquote>
== Public Modules ==
<blockquote>
All published packages are public by default.<br>
You can <code>retract</code> package versions if you discovered a mistake.<br>
Official package publishing instructions [https://go.dev/doc/modules/publishing here].
 
Release Preparation
<syntaxhighlight lang="bash">
go mod tidy
go test
git tag v0.0.1
git push origin v0.0.1
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Imports -->
 
Publishing your package
<syntaxhighlight lang="bash">
# adds your package to the index
go list
 
# add specific module-version to a specific-index
GOPROXY=proxy.golang.org \
  go list \
  -m example.com/mymodule@v0.1.0
</syntaxhighlight>
</blockquote><!-- Public Packages -->
 
== Private Modules ==
<blockquote>
Go executables are installed/built from src.<br>
You'll need to add your package to <code>$GOPRIVATE</code> envvar to stop it from being indexed.<br>
While unset <code>$GONOPROXY</code> and <code>$GONOSUMDB</code>, will fall back on <code>$GOPRIVATE</code>.<br>
Otherwise you'll want to set these to prevent your packages from getting added to the index.
 
See also [[golang module path]].
</blockquote><!-- Private Packages -->
</blockquote><!-- Creating Packages -->

Latest revision as of 14:16, 19 June 2022

Go modules are a collection of packages that are intended to be released together.
You define your project requirements in at the module level,
and you can publish module-versions to the go package index.

Documentation

private modules https://go.dev/ref/mod#private-modules
module proxies https://go.dev/ref/mod#module-proxy
module cache https://go.dev/ref/mod#module-cache

Locations

go module index https://index.golang.org/index
go checksum index https://sum.golang.org/

module path

The module path determines how your package is identified,
what the executable will be called,
how it will be downloaded,
and it can be filtered to ensure it is private.

example:

example.com/x/foo

See golang module path.

Go.mod file

The go.mod file stores metadata about your project, and it's requirements.
There are several tools to help manage it, but this is the source of truth.

Module Requirements

Tools

See also go.mod file .

go mod init example.com/x/myproject  # create a new module

go mod graph   # show requirements tree
go mod tidy    # ensure go.mod matches src (add requirements if necessary)
go mod vendor  #

# add/update requirements to project
go get example.com/module@latest  # update to latest
go get example.com/module v1.1.1  # update/downgrade to target version

Vendoring

Publishing Modules

Public Modules

All published packages are public by default.
You can retract package versions if you discovered a mistake.
Official package publishing instructions here.

Release Preparation

go mod tidy
go test
git tag v0.0.1
git push origin v0.0.1

Publishing your package

# adds your package to the index
go list

# add specific module-version to a specific-index
GOPROXY=proxy.golang.org \
  go list \
  -m example.com/mymodule@v0.1.0

Private Modules

Go executables are installed/built from src.
You'll need to add your package to $GOPRIVATE envvar to stop it from being indexed.
While unset $GONOPROXY and $GONOSUMDB, will fall back on $GOPRIVATE.
Otherwise you'll want to set these to prevent your packages from getting added to the index.

See also golang module path.