Skip to main content

How to Add a Dependency to a Go Module with go get

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Go projects use other people's packages by importing them, and go get is how a module adds one: it downloads the package's module and records the version in go.mod. After that, go run and go build use it like any other package.

Syntax​

Syntax
go get <module-path>[@<version>]
PartWhat it does
go getAdds a module to the current module's requirements in go.mod, and downloads it.
<module-path>The module or package to add, such as github.com/google/uuid.
@<version>Optional. A version such as @v1.6.0, or @latest, which is also what you get without it.

The Example​

The example program prints a new random UUID, a 36-character unique identifier, using Google's uuid package.

Prerequisites​

  • Go. Check it with go version. This guide uses Go 1.27.4.

Step 1: Create the Module and the Program​

Terminal
mkdir id-go
cd id-go
go mod init example.com/id-go

How to Create a Go Module with go mod init explains the last command. Create main.go, which imports the package by its path:

main.go, which imports fmt and github.com/google/uuid and prints uuid.NewString().

main.go
package main

import (
"fmt"

"github.com/google/uuid"
)

func main() {
fmt.Println(uuid.NewString())
}

Step 2: Add the Package​

Terminal
go get github.com/google/uuid
Output
go: downloading github.com/google/uuid v1.6.0
go: added github.com/google/uuid v1.6.0

Go downloaded version 1.6.0, the newest, and added it to the module. The command is the same in zsh, bash and PowerShell.

Step 3: Look at What Changed​

go.mod now requires the package's module:

go.mod
module example.com/id-go

go 1.27.4

require github.com/google/uuid v1.6.0 // indirect

And a new file, go.sum, holds a checksum for the downloaded code, so a later download that doesn't match is refused:

go.sum
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

Commit both files with your code.

The // indirect comment says, in the words of the Go Modules Reference, "that no package from the required module is directly imported by any package in the main module". That's not true here, since main.go imports it, but go get doesn't look at your code to decide. go mod tidy does: run it, and the line becomes require github.com/google/uuid v1.6.0. Either way, the program builds the same.

Step 4: Run the Program​

Terminal
go run .
Output
90e64c57-50e8-432b-9627-22c20021c948

Your UUID will differ: it's random each time.

A zsh terminal where go mod init creates example.com/id-go, go get github.com/google/uuid downloads and adds v1.6.0, and go run . prints a random UUID.

Common Mistakes​

MistakeWhat happensDo this instead
Importing a package without adding itgo run stops and suggests go getRun go get <module-path>
Leaving go.sum out of GitOthers get a checksum error, or must download and trust the code againCommit go.mod and go.sum
Expecting // indirect to mean the package isn't usedIt can appear on a package you import, right after go getRun go mod tidy to set the comments right

Troubleshooting​

SymptomLikely causeFix
no required module provides package github.com/google/uuidThe package isn't in go.modgo get github.com/google/uuid
// indirect next to a package your code importsgo get added it without looking at the codego mod tidy

FAQ​

How do I add a specific version?​

Put it after an @: go get github.com/google/uuid@v1.6.0. go get github.com/google/uuid@latest updates to the newest.

How do I remove a dependency?​

Delete the imports that use it, then run go mod tidy, which removes requirements that no code needs.

Conclusion​

go get <module-path> downloads a package's module and adds it to go.mod, with checksums in go.sum. After that, an import of the package works, and go mod tidy keeps the requirement lines in step with your code.

Additional Resources​