← Back to Go Course | Chapter 1: Setup & Basics | Lesson 5 of 7

Go Modules and go.mod

A go.mod file is like a program's ID card -- it names the project and lists every outside toolkit it depends on.
Syntax
go
module module/path

go 1.21

require example.com/pkg v1.2.3

What Is a Module?

A Go module is a collection of related packages that are versioned and distributed together, identified by a module path (often a repository URL like github.com/user/project).

Modules are Go's unit of dependency management, similar to a package.json in Node.js or a Cargo.toml in Rust.

Example: What Is a Module?

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println("A module groups packages under one versioned identity")
}

Creating go.mod with go mod init

Running 'go mod init' followed by a module path creates a go.mod file in the current directory.

This file records the module's name and the minimum Go version it requires, and it becomes the anchor that all import paths within the project are resolved against.

Note: Use a real or placeholder repository path, e.g. 'github.com/yourname/project', even for local-only projects.

Example: Creating go.mod with go mod init

bash
go mod init github.com/example/myapp

⚠️ Run this in your own terminal or Node.js environment.

Reading a go.mod File

A go.mod file lists the module directive (its name), a go directive (minimum Go version), and a require block listing each dependency module with its exact version. Go tooling reads this file automatically -- you rarely edit it by hand.

Example: Reading a go.mod File

bash
module github.com/example/myapp

go 1.22

require (
	github.com/some/dependency v1.4.0
)

⚠️ Run this in your own terminal or Node.js environment.

Adding Dependencies

When your code imports a package that isn't part of the standard library, you fetch and record it with 'go get'.

This updates go.mod with the new requirement and creates or updates go.sum, which stores cryptographic checksums to verify dependencies haven't been tampered with.

Note: 'go get' updates both go.mod and go.sum; never edit go.sum manually.

Example: Adding Dependencies

bash
go get github.com/some/[email protected]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Editing go.mod by hand to add a dependency instead of using 'go get', which leaves go.sum out of sync.
  2. Forgetting to run 'go mod init' before writing import statements for a new project, so there is no module to resolve packages against.
  3. Choosing a module path that doesn't match where the code is actually hosted, causing problems if the module is ever published.
Chapter Summary
  • 'go mod init <module-path>' creates a go.mod file, turning a directory into a Go module.
  • go.mod records the module's name, the Go version, and its dependencies with exact versions.
  • Modules replaced the older GOPATH workspace system as the standard way to manage Go projects.
  • Dependencies are added automatically by importing a package and running 'go build' or 'go get'.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.