"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Can You Completely Avoid Indirect Dependencies in Go Modules?

Can You Completely Avoid Indirect Dependencies in Go Modules?

Published on 2024-11-12
Browse:596

Can You Completely Avoid Indirect Dependencies in Go Modules?

Avoiding Indirect Dependencies in go.mod Files

In the context of Go modules, dependencies are packages that are required by a particular module. While direct dependencies are explicitly listed in the require statement of the go.mod file, indirect dependencies are automatically added when a direct dependency depends on them.

This can lead to a situation where a module's go.mod file contains numerous indirect dependencies, even though they are not directly used by the module itself. This can be confusing and difficult to manage, especially when multiple versions of a dependency are included.

Unfortunately, it is not possible to completely avoid indirect dependencies in go.mod files. When using Go modules, all dependencies required by a direct dependency will automatically be added as indirect dependencies, unless otherwise specified.

Take the example provided in the question:

module prodenv

go 1.13

require (
    github.com/gocolly/colly v1.2.0
    ...
)

In this case, github.com/gocolly/colly v1.2.0 does not have a go.mod file, so all of its dependencies are listed as indirect in the prodenv module's go.mod file.

To avoid indirect dependencies, it is recommended to use dependencies that have go.mod files. This will ensure that only the dependencies that are directly used by the module are listed in the go.mod file.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3