ProgramGuru

Introduction to Go Modules

Start with Introduction to Go Modules

A Go module is a set of related packages with a module path and dependency information. Modules make it easier to build and share projects consistently.

What you’ll learn

  • Define a Go module.
  • Initialize a module and read go.mod.
  • Distinguish modules from packages.

Understand the idea

Run go mod init example.com/hello in a new project directory to create go.mod. The module line records the module path; the go directive records the language-version requirement for that module.

When you later add external libraries, Go records required modules in go.mod and checksums in go.sum. The folder holding go.mod is the module root. For an unpublished learning project, an illustrative module path is sufficient.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for modules introThe module file describes the project, while packages hold executable Go code.Project folderStep 1go.mod metadataStep 2Go packagesStep 3Build / runStep 4The module file describes the project, while packages hold executable Go code.
The module file describes the project, while packages hold executable Go code.

Tools, syntax and practical notes

Initialize and run

mkdir hello
cd hello
go mod init example.com/hello
go run .

Save the example in main.go before go run .. See the official getting-started tutorial.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	fmt.Println("My first module")
}

Output

My first module

Trace what the program does

The entry point still looks like a normal Go program. The module file is project metadata, not a replacement for package main or main(). From the module root, go run . finds and runs the package.

Avoid common mistakes

  • A module and a package are not synonyms: a module can contain several packages.
  • A module path is not a filesystem directory name that Go automatically creates.

Try it yourself

Create a directory, initialize example.com/hello, save main.go, and run go run .. Open go.mod and identify its module directive.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What is the purpose of go.mod?
Choose one answer. You can try again.

Answer and explanation

It records the module path and dependency requirements

Correct. The entry point still looks like a normal Go program.

What you should remember

You have practiced Introduction to Go Modules in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Syntax →