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.
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 initTrace 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
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.