Chapter 2: Package Management
In Ruby, you install packages with gem install, manage dependencies with a Gemfile and Bundler, and share code across files with require. In Go, the equivalents are go get, go.mod, and import. Let’s map each one.
Try It Yourself
Sharing Go Packages
Go packages are namespaces for your functions, structs, and variables. import loads a package into your program — the equivalent of Ruby’s require. Every Go file starts with a package declaration, and you can import packages from the standard library or from remote repositories.
In the following example, we import several packages and use their functions by prefixing with the package name. We also show how to alias a package on import — similar to Ruby’s alias or assigning a module to a shorter constant.
1 package main
2
3 import (
4 "time"
5
6 "github.com/foo/greeter"
7 weather "github.com/bar/climate"
8 )
9
10 func main() {
11 greeter.SayHello("John") // Hello John!
12
13 weather.SayWeather(time.Now())
14 // It will be sunny today. Overall it's
15 // a good weather!
16 }
Notice that time and greeter are used as-is, while climate gets the alias weather. In Ruby terms, a Go package works like a module containing methods — you import the namespace, then call its exported functions.
1 module Coffee
2 def types
3 %w[
4 espresso
5 cappuccino
6 macchiato
7 ]
8 end
9 end
10
11 class Drink
12 include Coffee
13
14 def menu
15 puts types
16 end
17 end
18
19 drink = Drink.new
20
21 drink.menu
To rewrite this in Go, we create a local package. With Go Modules (the default since Go 1.16), your code can live anywhere — no GOPATH required:
1 package coffee
2
3 func Types() []string {
4 types := []string{"espresso", "cappuccino", "macchiato"}
5
6 return types
7 }
The capitalized Types is exported — Go’s rule is simple: capitalize to export, lowercase to keep private. This applies to functions, structs, and variables alike. Next, we import the package and call its exported functions:
1 package main
2
3 import (
4 "fmt"
5
6 "local/coffee"
7 )
8
9 func main() {
10 menu()
11 }
12
13 func menu() {
14 for _, val := range coffee.Types() {
15 fmt.Println(val)
16 }
17 }
That covers the basics of packages and imports. Now let’s manage external dependencies — Go’s equivalent of a Gemfile and Bundler.
Try It Yourself
Go Modules
Go Modules is Go’s built-in dependency manager — think Bundler, but compiled into the go command itself. It’s been the default since Go 1.16 (released 2021), and in current Go versions (1.22+) it’s the only workflow. No environment variables to set, no special directory layout required.
To start a new project, create a directory anywhere on disk and run:
1 $ mkdir myapp && cd myapp
2 $ go mod init example.com/myapp
3 go: creating new go.mod: module example.com/myapp
This creates a go.mod file — Go’s Gemfile. When you import an external package and run go mod tidy, Go downloads the dependency, records the exact version in go.mod, and generates a go.sum file (Go’s Gemfile.lock) with cryptographic hashes of every dependency.
For remote packages, Go resolves imports directly from the module path. Import github.com/fatih/color in your code, run go mod tidy, and Go fetches it automatically — no separate gem install step.
There are two ways to set up dependencies: write the go.mod by hand, or let Go generate it from your source code.
Try It Yourself
Manual go.mod Generation
We will first discuss how to create the go.mod file manually. In the terminal, we will be creating the go.mod file on the root of our ~/code/menu directory.
1 $ cd ~/code/menu
2
3 $ cat >go.mod <<EOF
4 module menu
5
6 require github.com/foo/coffee v0.0.1
7 EOF
Inside the go.mod, we must specify the required package with the version number. For multiple requirements, you can use the require(...) statement. Versions can support pattern matching via semantic version prefix. For example, having the version to v1 means using the latest available tagged version with that prefix. It can also support version comparison via <, <=, >, and >=. Lastly, you can just specify latest as the version, and it will pick the latest tagged or latest commit. After the go.mod file is created, we can now run our project via go run ., which will automatically download the dependencies.
1 $ cd ~/code/menu
2 $ go run .
3 go: finding github.com/foo/coffee v0.0.1
4 go: downloading github.com/foo/coffee v0.0.1
5
6 espresso
7 cappuccino
8 macchiato
You can also download the dependencies manually via go get -u. If you make the ~/code/menu API public and Go Modules ready, you have to change the module line in the go.mod file to point to a remote resource. The module menu needs to be pointing to a remote resource, i.e. module github.com/foo/menu.
Try It Yourself
Automatic go.mod Generation Through Source-Code
To generate a go.mod through its source-code, you can use the go mod tidy, automatically creating the go.mod based on the package import.
However, if you only import part of a package, i.e., github.com/quux/foo/baz, and not requiring the repo package github.com/quux/foo in your packages, github.com/quux/foo/baz will be treated as a repository. To make your apps module-aware in the future, baz either needs to be:
1. in its separate repository, i.e., github.com/quux/baz.
2. marked as // indirect in the go.mod file, for example:
In your project file foo.go, you have an import statement:
1 import "github.com/quux/foo/baz"
Then if running go mod tidy gives you the following error:
1 $ go mod tidy
2
3 go: finding github.com/quux/foo/baz latest
4 go: import "github.com/quux/foo/baz" ->
5 import "github.com/quux/foo/baz":
6 cannot find module providing package github.com/quux/foo/baz
We have to manually add the failing package in the go.mod file, but it must fill in the semantic version to get the latest version via curl and jq.
1 $ curl --silent
2 "https://api.github.com/repos/**quux/foo**/releases/latest"\
3 | jq -r .tag_name
4
5 v0.0.1
Once you get the version tag, add it to the go.mod file and comment as // indirect.
1 module github.com/foo/bar
2
3 require (
4 ...
5 github.com/quux/foo v0.0.1 // indirect
6 )
Then run go mod tidy again.
If you find that you have too many // indirect comments, you might want to consider option A, since if the package is used on different responsibilities, it might be logical for the package to have its own repo as a reusable package.⁴
Try It Yourself
Refresh Go Modules
Go stores downloaded modules in a local cache. If your dependencies get into a weird state, you can clear it:
1 $ go clean -modcache
2 $ go mod tidy # re-downloads everything
The cache lives at $GOPATH/pkg/mod (default: ~/go/pkg/mod) and is read-only by design. You rarely need to touch it directly.
Go Workspaces
Go 1.18 introduced workspaces for working on multiple modules simultaneously — useful when you’re editing a library and the application that uses it at the same time:
1 $ go work init ./myapp ./mylib
This creates a go.work file that tells Go to resolve imports from your local ./mylib directory instead of downloading them. It’s like Ruby’s path: option in a Gemfile, but at the workspace level. When you’re done, delete go.work or run go work sync.
A Note on dep
Before Go Modules, the Go community used dep for dependency management. You may still encounter it in projects created before 2019 — look for Gopkg.toml and Gopkg.lock files. To migrate a dep project to modules:
1 $ go mod init
2 $ go mod tidy # reads Gopkg.lock to seed go.mod
dep is no longer maintained and should not be used for new projects. Go Modules are the standard.
Try It Yourself
Chapter Exercises
Summary
Go Modules is the standard. Start every project with go mod init. Dependencies are declared in go.mod (your Gemfile) and locked in go.sum (your Gemfile.lock). go mod tidy cleans up unused dependencies and downloads missing ones — it’s bundle install and bundle clean in one command.
Capitalize to export, lowercase to keep private. Import remote packages directly from their module path — no separate gem server or registry needed. And if you ever see GOPATH or dep in old tutorials, you now know: those are legacy.
Now that you can organize Go code into packages and manage dependencies, it’s time to look at Go’s type system. In the next chapter, you’ll learn how structs group data and attach behavior — Go’s answer to Ruby classes and instance variables.