ProgramGuru

Introduction to Go Modules

Start with Introduction to Go Modules

A module is a versioned collection of one or more Go packages with a declared module path and dependency information.

What you’ll learn

  • Explain the main purpose of introduction to go modules.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Modern Go projects typically start with go mod init example.com/myapp. The generated go.mod declares the module path and the Go language version. A single module can contain several packages in subdirectories.

The module path establishes the prefix of import paths for packages inside it. A local learning module does not need to exist on a website, but reusable published module paths should identify a resolvable repository location.

Visualize how it works

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

Visual explanation for modulesgo.mod identifies a module and its dependency graph.Project directoryStep 1go mod initStep 2go.modStep 3Module packagesStep 4go.mod identifies a module and its dependency graph.
go.mod identifies a module and its dependency graph.

Tools, syntax and practical notes

Terminal

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

Run the final command once main.go has been created.

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"
	"strings"
)

func main() {
	fmt.Println(strings.Join([]string{"go", "mod", "init"}, " "))
}

Output

go mod init

Trace what the program does

This executable uses only standard-library packages, so no downloaded dependency is needed. Module metadata lives outside the source file; the example prints the command used to initialize it.

Avoid common mistakes

  • The module path is not necessarily a package declaration.
  • Creating a module does not mean you published it to a remote registry.

Try it yourself

Make a new folder, run go mod init example.com/myapp, save a main.go file, then run go run ..

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

Check your understanding

Which file identifies the root of a Go module?
Choose one answer. You can try again.

Answer and explanation

go.mod

Correct. This executable uses only standard-library packages, so no downloaded dependency is needed.

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: Understanding go.mod →