last modified August 24, 2023
Go module tutorial shows how to work with modules in Golang.
$ go version go version go1.18.1 linux/amd64
We use Go version 1.18.
A module is a collection of Go packages stored in a file tree.
go.modgo.sumgo.modgo.sum
Modules allow us to define precise dependency requirements and design reproducible builds for multiple environments.
go
The go module commands
The go tool provides several commands that are related to modules.
- go mod init - initializes new module in current directory
- go mod tidy - adds missing and removes unused modules
- go mod download - downloads modules to local cache
- go mod vendor - makes vendored copy of dependencies
- go mod graph - prints module requirement graph
- go mod verify - verifies dependencies have expected content
- go mod why - explains why packages or modules are needed
There are additional commands related to Go modules.
go list -mgo getgo buildgo test
The go mod init
go mod initgo.mod
$ go mod init [module-path]
The module path is the prefix path that is used to import all packages of that module. Developers often use the URL of the repository that hosts the source code as the module path.
$ go mod init com.zetcode/first go: creating new go.mod: module com.zetcode/first
com.zetcode/first
$ cat go.mod module com.zetcode/first go 1.17
go.mod
The go get command
go getgo.modgo.sum
$ go mod init com.zetcode/myprog
First, we create a module.
$ go get gonum.org/v1/gonum/stat go: downloading gonum.org/v1/gonum v0.9.3 go get: added gonum.org/v1/gonum v0.9.3
We install the Gonum library. It is a set of numeric libraries for the Go language.
$ ls go.mod go.sum
go.modgo.sum
~/go/pkg/mod/gonum.org/v1/gonum@v0.9.3$ ls -1 appveyor.yml AUTHORS blas cmplxs CONDUCT.md CONTRIBUTING.md CONTRIBUTORS diff doc.go dsp floats go.mod GOPHER gopher.png gopher.svg go.sum graph integrate internal interp lapack LICENSE mat mathext num optimize README.md spatial stat THIRD_PARTY_LICENSES unit version.go
go
package main import ( "fmt" "math" "gonum.org/v1/gonum/stat" ) func main() { xs := []float64{ 12.44, 11.2, 34.5, 1.4, 6.7, 23.4, } fmt.Printf("data: %v\n", xs) mean := stat.Mean(xs, nil) variance := stat.Variance(xs, nil) stddev := math.Sqrt(variance) fmt.Printf("mean: %v\n", mean) fmt.Printf("variance: %v\n", variance) fmt.Printf("std-dev: %v\n", stddev) }
We have a simple example that computes some basic statistics.
import ( "fmt" "math" "gonum.org/v1/gonum/stat" )
stat
$ go run first.go data: [12.44 11.2 34.5 1.4 6.7 23.4] mean: 14.94 variance: 145.12640000000002 std-dev: 12.046841909811883
$ go list -m all com.zetcode/myprog dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9 gioui.org v0.0.0-20210308172011-57750fc8a0a6 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af github.com/boombuler/barcode v1.0.0 github.com/davecgh/go-spew v1.1.0 ...
go list -m all
Vendoring
vendor
$ go mod vendor
go mod vendorvendor/modules.txt
$ go mod init com.zetcode/second $ go get github.com/bykof/gostradamus
We create a project and download a dependency. The gostradamus is a library for working with date and time.
package main import ( "fmt" "github.com/bykof/gostradamus" ) func main() { now := gostradamus.Now() fmt.Println(now) }
The example prints the current datetime.
$ go mod vendor
We initialize vendoring.
$ ls go.mod go.sum second.go vendor
vendor
$ ls vendor/github.com/ bykof
It contains our dependency.
In this article we have talked about modules in Go.
Author
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.