Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. Go Modules

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

CommandDescription
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 tidyRemoves unused dependencies and updates go.mod and go.sum.
go mod vendorCopies dependencies into the vendor/ directory.
go mod downloadDownloads dependencies to the local cache.
go mod verifyVerifies the integrity of downloaded modules.
go mod graphPrints the dependency graph.
go list -m allLists 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 .