Go Modules
Go Modules is the official tool for managing dependencies. It creates a go.mod file in your project's root directory to track external package versions. Use go mod init to initialize a module, and go get to add dependencies.
Syntax
// Initialize a module (creates go.mod).
// go mod init module-name
// Example:
// go mod init github.com/yourname/myapp
// Add a dependency (updates go.mod and go.sum).
// go get github.com/gin-gonic/gin
// Specify a particular version.
// go get github.com/gin-gonic/gin@v1.9.0
// Clean up dependencies to match go.mod (removes unused entries).
// go mod tidy
// Create a vendor directory (for offline use).
// go mod vendor
// Example contents of go.mod.
/*
module github.com/yourname/myapp
go 1.21
require (
github.com/gin-gonic/gin v1.9.0
golang.org/x/text v0.12.0
)
*/
Command Reference
| Command | Description |
|---|---|
| go mod init [module] | Initializes a new module and creates go.mod. |
| go get [pkg] | Adds or updates a dependency. |
| go get [pkg]@[version] | Fetches a dependency at a specific version. |
| go mod tidy | Removes unused dependencies and updates go.mod and go.sum. |
| go mod vendor | Copies dependencies into the vendor/ directory. |
| go mod download | Downloads dependencies to the local cache. |
| go mod verify | Verifies the integrity of downloaded modules. |
| go mod graph | Prints the dependency graph. |
| go list -m all | Lists all modules and their versions. |
Sample Code
// --- Setting up a new project ---
// 1. Create a directory and initialize the module.
// mkdir myapp && cd myapp
// go mod init github.com/yourname/myapp
// 2. The generated go.mod looks like this.
/*
module github.com/yourname/myapp
go 1.21
*/
// 3. Add an external package.
// go get golang.org/x/text@latest
// 4. go.mod is updated automatically.
/*
module github.com/yourname/myapp
go 1.21
require golang.org/x/text v0.14.0
*/
// 5. go.sum records a hash for each package.
/*
golang.org/x/text v0.14.0 h1:ScX5w...
golang.org/x/text v0.14.0/go.mod h1:...
*/
// --- Using a package in main.go ---
package main
import (
"fmt"
// Example using only standard library packages (no external dependencies).
"strings"
"unicode"
)
func main() {
// Go Modules manages external packages.
// Standard library packages do not require module management.
s := "Hello, Go Modules!"
words := strings.Fields(s)
fmt.Printf("Word count: %d\n", len(words))
// Use the unicode package to check for uppercase letters.
for _, r := range s {
if unicode.IsUpper(r) {
fmt.Printf(" Uppercase: %c\n", r)
}
}
}
Notes
Go Modules was introduced in Go 1.11 and is enabled by default since Go 1.16. The go.mod file records the module name, Go version, and required packages. The go.sum file stores a hash for each package. Both files should be committed to version control.
It is recommended to run go mod tidy regularly during development. It removes references to unused packages and keeps go.mod and go.sum up to date.
Do not delete or manually edit go.sum. It is used for integrity checks, and corrupting it will cause go build and go get to fail.
For how to import packages, see Package / import. For build commands, see go build / run / test.
If you find any errors or copyright issues, please contact us.