Go > Packages and Modules > Go Modules > Managing dependencies (go get, go mod tidy)

Go Modules: Managing Dependencies

This example demonstrates how to use Go modules to manage dependencies in a Go project. It covers the basic usage of go mod init, go get for adding dependencies, and go mod tidy for cleaning up the module.

Initializing a Go Module

First, you need to initialize a Go module. This command creates a go.mod file in your project's root directory. Replace mymodule with your desired module name. The module name typically follows the format of your repository URL (e.g., github.com/yourusername/yourproject).

go mod init mymodule

Adding a Dependency with go get

The go get command is used to add a dependency to your project. In this example, we are adding the gin web framework. This command downloads the package and updates your go.mod and go.sum files.

go get github.com/gin-gonic/gin

Using the Dependency

This code snippet demonstrates how to import and use the gin framework. It creates a simple web server that responds with 'pong' when you visit the /ping endpoint.

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})

	r.Run() // listen and serve on 0.0.0.0:8080
}

Cleaning up Dependencies with go mod tidy

The go mod tidy command removes unused dependencies from your go.mod file and adds any missing dependencies. It's a good practice to run this command regularly to keep your module file clean.

go mod tidy

Real-Life Use Case

Imagine you're building a REST API. You'll likely need dependencies for routing (like gin or mux), database interaction (like gorm or sqlx), and JSON handling. Go modules allow you to easily add, update, and manage these dependencies.

Best Practices

  • Always initialize a module at the beginning of a project.
  • Use explicit versions when adding dependencies with go get (e.g., go get github.com/gin-gonic/gin@v1.9.1). This ensures reproducibility.
  • Run go mod tidy regularly.
  • Commit your go.mod and go.sum files to version control.

When to Use Go Modules

Go modules are the recommended way to manage dependencies in Go projects. Use them for all new projects and consider migrating older projects to Go modules.

Alternatives

Before Go modules, GOPATH was the standard way to manage dependencies. Tools like dep were also used. However, Go modules are now the preferred solution and offer significant advantages over older methods.

Pros of Go Modules

  • Version management: Ensures reproducible builds.
  • Dependency isolation: Avoids dependency conflicts.
  • Simplified dependency management: Easy to add, update, and remove dependencies.

Cons of Go Modules

  • Initial setup: Requires understanding of module paths and versions.
  • Module path conflicts: Can occur if module paths are not unique.

FAQ

  • What is the go.mod file?

    The go.mod file is a text file that describes the module's dependencies. It contains information about the module's name, the Go version used, and the dependencies required by the module.
  • What is the go.sum file?

    The go.sum file contains cryptographic hashes of the dependencies used by the module. It's used to verify that the downloaded dependencies have not been tampered with.
  • How do I update a dependency?

    Use go get with the desired version (e.g., go get github.com/gin-gonic/gin@latest). Then run go mod tidy.