SoFunction
Updated on 2025-03-05

Use of go mod tidy command

go mod tidyis a Go command used to clean up and updateanddocument. It has the following main functions:

  • Remove unused dependencies:fromDelete those dependencies that are no longer used in the code in the file.
  • Add missing dependencies: Added code to use but not documented yetDependencies in the file.
  • renewdocument:make sureThe file contains the correct checksum of all dependencies.

Example

Suppose you have a projectThe file is as follows:

module /mymodule

go 1.18

require (
    /sirupsen/logrus v1.8.1
    /stretchr/testify v1.7.0
)

But in the code, you actually only use/sirupsen/logrus, without using/stretchr/testify

Rungo mod tidyAfter the order,The file will be updated, only the actual dependencies used are retained:

module /mymodule

go 1.18

require /sirupsen/logrus v1.8.1

How to use

In the project root directory, run the following command:

go mod tidy

This command will automatically analyze all code in the project and updateandFiles, make sure they are consistent with the dependencies used by the actual code.

Function summary

  • Clean up dependencies: Remove unused dependencies and keep the project clean and tidy.
  • Completion of dependencies: Add dependencies used in the code but not documented.
  • Update checksum:make sureThe file contains the correct checksum of all dependencies.

By usinggo mod tidy, which ensures that the project's dependencies are accurate and helps maintain and manage module dependencies in Go projects.

This is the end of this article about the use of go mod tidy command. For more related go mod tidy content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!