Home > Article > Backend Development > Can You Completely Avoid Indirect Dependencies in Go Modules?
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.
The above is the detailed content of Can You Completely Avoid Indirect Dependencies in Go Modules?. For more information, please follow other related articles on the PHP Chinese website!