Installing Go and Understanding Modules
Getting the Go toolchain set up and understanding how Go manages dependencies with modules.
2 min read
Before writing any Go code, you need the Go toolchain — the compiler, formatter, package manager, and test runner all bundled into one go command. Unlike some ecosystems, there's no separate package manager to install; it all ships together.
Installing Go
Head to go.dev/dl and download the installer for your operating system, or use a package manager:
# macOS (Homebrew)
brew install go
# Windows (winget)
winget install GoLang.Go
# Linux (varies by distro, e.g. apt)
sudo apt install golang-goOnce installed, confirm it worked:
go versionThat should print something like go version go1.22.0 darwin/amd64. If the command isn't found, your PATH doesn't include Go's bin directory yet — the installer usually handles this, but it's the first thing to check if go commands fail.
What "modules" solve
Every real Go project depends on other packages — maybe a web framework, a database driver, a testing library. Go modules are how those dependencies are declared, versioned, and downloaded, similar to package.json in Node or Cargo.toml in Rust.
A module is defined by a go.mod file at the root of your project. You create one with:
go mod init github.com/yourname/yourprojectThat name doesn't have to be a real GitHub URL for a personal project, but using a real (or plausible) module path matters the moment anyone else needs to import your code, so it's worth getting into the habit early.
Reading a go.mod file
module github.com/yourname/yourproject
go 1.22
require (
github.com/gin-gonic/gin v1.9.1
)
moduledeclares the module's import path.go 1.22pins the minimum Go language version the module expects.requirelists direct dependencies and their exact versions.
Adding and managing dependencies
You rarely edit go.mod by hand. Instead, you import a package in your code and run:
go get github.com/gin-gonic/ginThis downloads the package, adds it to go.mod, and records exact resolved versions (including transitive dependencies) in a companion go.sum file, which acts like a lockfile — it ensures everyone building the project gets byte-for-byte identical dependency code. Commit both files to version control.
To clean up unused dependencies or add missing ones after editing imports by hand, run:
go mod tidyThis is worth running before every commit; it keeps go.mod and go.sum honest about what your code actually imports.
Where dependencies live
Downloaded module code is cached globally (usually under $GOPATH/pkg/mod), not inside each project's folder like node_modules. That means switching between projects doesn't re-download shared dependencies, and your project directory stays lean — just your source files, go.mod, and go.sum.
With the toolchain installed and modules understood, you're ready to write and run actual Go code, which is exactly what the next lesson does.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.