Level: BeginnerGo Programs

Go Dependencies Explained: go get, go.mod and go.sum

Updated 14 min read
Go Dependencies featured image: go mod tidy and go get in a terminal

Go dependencies are the packages your program borrows from other people, such as a UUID maker or a web router. Go has a built-in system to add, update and lock them, so every build gets the same code. In this post, you will learn go get, go.mod, go.sum and go mod tidy with real programs, and then practice with common interview questions.

This is the fourth post in our Go programs series. You should already know how to run a Go program inside a module folder. If you want the theory of packages first, read our Go modules and packages guide.

What are Go Dependencies?

A dependency is code your project needs but did not write. In Go, dependencies come as modules. A module is a group of packages with a name, like github.com/google/uuid, and a version, like v1.6.0.

Think of cooking at home. You make the dish, but you buy the flour and the spices from a shop. Your go.mod file is the shopping list, and go.sum is the receipt that proves you got the exact items you paid for.

Why Go Dependencies Matter

Almost every real Go project uses outside code. So managing Go dependencies well is part of daily work.

  • Same build everywhere: a teammate, a CI server and your laptop all use the same versions.
  • Safety: checksums stop anyone from swapping the code behind a version.
  • Easy updates: one command moves a package to a newer version.
  • Clean code: go mod tidy removes packages you no longer import.

How Go Dependencies Work

When you import a new package and run go mod tidy, the Go dependencies system finds the right version, downloads it, and writes it down. The flow is the same for every package.

Go dependencies diagram showing import, go mod tidy, module proxy, go.mod, go.sum and module cache
Go reads your imports, downloads modules through the proxy, records them in go.mod and go.sum, and keeps a copy in the module cache.
  1. You add an import line for a package you do not have yet.
  2. You run go mod tidy or go get.
  3. Go asks the module proxy, proxy.golang.org, for the module.
  4. Next, Go checks the download against the checksum database.
  5. Finally, Go writes the version in go.mod, the checksum in go.sum, and saves the code in the module cache.

Your First Program with Go Dependencies

Easy Start: A Program with No Go Dependencies

Before we add outside code, look at a program that needs none. It only uses the standard library, which comes with Go.

package main

import (
    "fmt"
    "strings" // strings is part of the standard library, so no download is needed
)

// main turns a word into capital letters and counts its letters
func main() {
    word := "gopher" // := creates a new variable

    upper := strings.ToUpper(word) // ToUpper returns a new, upper case copy
    fmt.Println(upper)
    fmt.Println("Letters:", len(word)) // len counts the letters in the word
}
GOPHER
Letters: 6

First, strings.ToUpper makes a capital letter copy of the word. Then len counts its letters. Because strings ships with Go, your go.mod stays short, with no require line at all.

Add Your First Outside Package

Now let’s build a small program that uses a public package. We will use rsc.io/quote, the same package from the official Go tutorial. First, create the module.

mkdir quotes
cd quotes
go mod init example.com/quotes

Next, write main.go. It imports the outside package and prints one of its quotes.

package main

import (
    "fmt"

    "rsc.io/quote" // an outside package, so Go must download it
)

// main prints one quote from the quote package
func main() {
    message := quote.Go() // Go() returns a famous Go saying as a string
    fmt.Println(message)
}

If you run it now, Go says the package is missing. So we ask Go to find it.

$ go mod tidy
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2

$ go run .
Don't communicate by sharing memory, share memory by communicating.

go mod tidy read the import, found the latest version and downloaded it. That is all it takes to add Go dependencies. After that, go run . worked on the first try.

Inside the go.mod File

Open go.mod after the command. Go has added the new package and the packages it needs in turn. The go line is set by go mod init. Since Go 1.26, it writes the release before yours, so Go 1.27 writes go 1.26.0.

module example.com/quotes

go 1.26.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
)

The table below explains each part of the file.

LineWhat it means
moduleThe name of your module and its import path
goThe lowest Go version this module needs
requireA dependency and its exact version
// indirectNeeded by a dependency, not imported by you
replaceUse a different copy, such as a local folder
excludeNever use this one version
toolA Go tool this module uses, run with go tool

What is go.sum?

For Go dependencies, go.sum holds a checksum for every module version your build uses. A checksum is a short fingerprint of the files. If the code behind a version ever changes, the fingerprint no longer matches, and Go stops the build.

rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=

So always commit both go.mod and go.sum to Git. Never edit go.sum by hand.

go.mod vs go.sum

Beginners often mix up these two files, so here is a side-by-side view.

Go dependencies comparison of go.mod and go.sum files
go.mod lists which versions you want, and go.sum proves the downloaded code has not changed.
Featurego.modgo.sum
PurposeLists modules and versionsStores checksums
Written byYou and GoGo only
Edit by handSometimes, carefullyNever
Commit to GitYesYes
Read by peopleYes, it is shortRarely

In short, go.mod says what you want, and go.sum proves you got it.

Managing Go Dependencies with go get

go get adds, upgrades, downgrades and removes Go dependencies. You add @ and a version to choose exactly what you want.

# add the latest version
go get github.com/google/uuid@latest

# pick an exact version
go get rsc.io/quote@v1.5.1

# upgrade one package and its own dependencies
go get -u github.com/google/uuid

# remove a dependency
go get github.com/google/uuid@none

Here is a second program that uses the uuid package. It creates a random ID, so your output will be different each time.

package main

import (
    "fmt"

    "github.com/google/uuid" // outside package that makes unique IDs
)

// main creates one random ID and prints it with its length
func main() {
    // Step 1: make a new random ID
    id := uuid.New()

    // Step 2: turn the ID into text like "7c9e6679-..."
    orderID := id.String()

    // Step 3: print the text and how many characters it has
    fmt.Println("Order ID:", orderID)
    fmt.Println("Length:", len(orderID))
}
$ go get github.com/google/uuid@latest
go: added github.com/google/uuid v1.6.0

$ go run .
Order ID: 7c9e6679-7425-40de-944b-e07fc1f90ae7
Length: 36

The ID changes on every run, but the length is always 36 characters. That makes it a good choice for order numbers and database keys.

Go Dependencies Commands Cheat Sheet

The table below lists the commands you will use most often to manage Go dependencies.

CommandWhat it does
go mod init nameCreates a new go.mod
go mod tidyAdds missing and removes unused modules
go get pkg@versionAdds or changes a dependency
go list -m allLists every module in the build
go list -m -u allShows which modules have updates
go mod why pkgShows why a package is needed
go mod graphPrints the full dependency tree
go mod verifyChecks the cache against go.sum
go mod vendorCopies dependencies into a vendor folder
go clean -modcacheDeletes the whole module cache

Major Versions in Go Dependencies

Go modules use semantic versions, written as vMAJOR.MINOR.PATCH. A new patch fixes bugs, and a new minor adds features. A new major version may break old code, so Go treats it as a new import path that ends in /v2, /v3 and so on.

Because of this rule, you can even use two major versions in one program. The example below imports v1 and v3 of the same module.

package main

import (
    "fmt"

    "rsc.io/quote"            // version 1 of the module
    quoteV3 "rsc.io/quote/v3" // version 3; quoteV3 is the name we use for it in this file
)

// main prints the same saying from two major versions of one module
func main() {
    fmt.Println(quote.Go())     // from v1
    fmt.Println(quoteV3.GoV3()) // from v3
}
Don't communicate by sharing memory, share memory by communicating.
Don't communicate by sharing memory, share memory by communicating.

We gave the v3 package the name quoteV3, so the two imports do not clash. This is how big projects move to a new major version step by step.

Easy Example: Give an Import a Short Name

The quoteV3 trick works for any import, even standard ones. This small program renames strings to text, so you can try the idea without any Go dependencies.

package main

import (
    "fmt"
    text "strings" // text is our own short name for the strings package
)

// main uses the renamed package to join and repeat words
func main() {
    words := []string{"go", "is", "fun"} // a slice, which is a list of strings

    sentence := text.Join(words, " ") // Join puts a space between the words
    fmt.Println(sentence)

    line := text.Repeat("-", 10) // Repeat makes ten dashes
    fmt.Println(line)
}
go is fun
----------

The name before the import path becomes the name you type in the code. So text.Join is the same function as strings.Join. Use this only when two imports clash, because the normal name is easier to read.

Local Changes, Private Code and Tools

Sometimes you need to change one of your Go dependencies, or you work on two modules at once. A replace line points Go to a folder on your disk instead of the internet.

replace example.com/greet => ../greet

For private company code, set GOPRIVATE, so Go skips the public proxy for those paths. For example, go env -w GOPRIVATE=github.com/my-company/* keeps private module names off the public servers.

Since Go 1.24, go.mod can also track tools. Run go get -tool golang.org/x/tools/cmd/stringer, and then run it with go tool stringer. As a result, every teammate uses the same tool version.

Common Mistakes with Go Dependencies

  • Not committing go.sum: other machines then fail with a “missing go.sum entry” error.
  • Editing go.sum by hand: let Go manage it, and run go mod tidy if it looks wrong.
  • Forgetting /v2 in the import: a v2 module needs the /v2 path, or Go keeps using v1.
  • Leaving a local replace line: a replace to ../greet breaks the build on other machines.
  • Skipping go mod tidy: unused modules stay in go.mod and slow down reviews.

Best Practices for Go Dependencies

  • Run go mod tidy before every commit, so go.mod matches your imports.
  • Pick packages with care, because each one adds code you must trust and update.
  • Update on purpose, with go list -m -u all and one upgrade at a time.
  • Prefer the standard library when it already does the job.
  • Use govulncheck to find known security bugs in the versions you use.

Go Dependencies Interview Questions and Answers

These are the dependency and module questions that come up most in Go interviews. Several include a program or command output, so you can try them yourself.

Go Dependencies Basics

Q1. What is a Go module?

A module is a group of Go packages that are versioned together. It has a go.mod file at its root that names the module and lists its dependencies.

Q2. What is the difference between a package and a module?

A package is one folder of .go files with the same package name. A module is a set of packages released together under one version. So one module can hold many packages.

Q3. What does go mod init do?

It creates a new go.mod file with the module path you give it, plus a go version line. For example, go mod init example.com/quotes starts a new module.

Q4. What does go mod tidy do?

It reads every import in your code. Then it adds missing modules, removes unused ones and updates go.sum. Run it whenever you add or remove imports.

Q5. What is the purpose of go.sum?

It stores checksums for each module version. Go compares downloads with these checksums, so nobody can change the code behind a version without the build failing.

Q6. What does // indirect mean in go.mod?

It marks a module that your code does not import directly. One of your dependencies needs it instead. Go still records it so the build stays the same everywhere.

Version Questions

Q7. How does Go pick a version when two packages need different ones?

Go uses Minimal Version Selection. If one package needs v1.2.0 and another needs v1.4.0, Go picks v1.4.0. It chooses the lowest version that meets every requirement, not the newest one on the internet.

Q8. How do you upgrade all dependencies?

Run go get -u ./... to move direct and indirect modules to newer minor or patch versions. Then run go mod tidy and your tests. For only patch updates, use go get -u=patch ./....

Q9. How do you remove a dependency?

Delete the imports from your code, then run go mod tidy. You can also run go get example.com/pkg@none.

Q10. Why does a v2 module need /v2 in its import path?

A major version can break old code. So Go treats v2 as a different module with its own path. This lets old and new versions live in one build without conflict.

Q11. What is a pseudo-version?

It is a version made from a commit when no tag exists, such as v0.0.0-20170915032832-14c0d48ead0c. It holds a time stamp and the commit hash.

Program and Command Output Questions

Q12. What does this program print?

package main

import (
    "fmt"

    "rsc.io/quote"
)

// main checks that the quote is not empty
func main() {
    message := quote.Go()
    fmt.Println(len(message) > 0) // > gives true or false
}
true

quote.Go() returns a non-empty string, so the comparison is true. However, the program only builds after go mod tidy adds rsc.io/quote to go.mod.

Q13. What happens if you build without running go mod tidy first?

main.go:6:5: no required module provides package rsc.io/quote; to add it:
    go get rsc.io/quote

Go refuses to download new modules during a normal build. It tells you the exact command to run, which keeps builds predictable.

Q14. What does go mod verify print when all is well?

all modules verified

It checks the code in your module cache against go.sum. If someone changed a file in the cache, it reports which module failed.

Q15. How do you see why a package is in your build?

Run go mod why with the package path. For example, go mod why golang.org/x/text/language prints the import chain from your code to that package.

Proxy, Vendor and Workspace Questions

Q16. What is GOPROXY?

It is the list of servers Go uses to download modules. The default is https://proxy.golang.org,direct, which means “try the proxy, then the source repo”. Companies often run their own proxy.

Q17. What is GOSUMDB?

It is the checksum database that Go checks the first time it sees a module version. The default is sum.golang.org. It makes sure everyone gets the same code for the same version.

Q18. What is GOPRIVATE used for?

It lists module paths that are private, such as github.com/my-company/*. For these paths, Go skips the public proxy and checksum database and goes straight to your repo.

Q19. What is vendoring?

go mod vendor copies all dependencies into a vendor folder in your project. Then builds use that folder, so they work without network access. Some teams use it for strict offline builds.

Q20. What is the replace directive used for?

It tells Go to use a different source for a module, such as a local folder or a fork. It is handy while you fix a bug in a dependency. Remove it before you release.

Q21. What is a Go workspace?

A workspace lets you work on many modules at once. You create it with go work init and add folders with go work use. The go.work file replaces most local replace lines during development.

Q22. Where are downloaded modules stored?

They live in the module cache, by default $GOPATH/pkg/mod. You can print it with go env GOMODCACHE and clear it with go clean -modcache.

Q23. How do you track a tool like stringer in go.mod?

Since Go 1.24, run go get -tool golang.org/x/tools/cmd/stringer. Go adds a tool line to go.mod, and go tool stringer runs that exact version.

Practice Task

Create a new module and use github.com/google/uuid to print five order IDs in a loop. Then remove the package with go get github.com/google/uuid@none and see what go build says. Hint: run go mod tidy after you add the import.

Official Resources

Quick Recap

  • Go dependencies are outside modules your code imports, each with a fixed version.
  • go.mod lists what you need, and go.sum stores checksums that prove it.
  • go mod tidy adds missing modules and removes unused ones.
  • go get pkg@version adds, upgrades, downgrades or removes a module.
  • Major versions from v2 onward need a /v2 style import path.

What’s Next?

That completes the setup level of our Go programs series. Coming soon: Go Hello World and print programs, where we start writing real programs with output. Until then, test yourself with the Go modules interview questions or browse the Go interview questions hub.