简体   繁体   中英

How to avoid indirect dependencies in go.mod file

I am running go build to get my go.mod file updated with library I am using "github.com/gocolly/colly v1.2.0" But I see all other dependencies saying "// indirect" at end. How to avoid getting this? Here is my go.mod file

   module prodenv

go 1.13

require (
   github.com/PuerkitoBio/goquery v1.5.1 // indirect
   github.com/antchfx/htmlquery v1.2.2 // indirect
   github.com/antchfx/xmlquery v1.2.3 // indirect
   github.com/antchfx/xpath v1.1.5 // indirect
   github.com/gobwas/glob v0.2.3 // indirect
   github.com/gocolly/colly v1.2.0
   github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
   github.com/kennygrant/sanitize v1.2.4 // indirect
   github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
   github.com/temoto/robotstxt v1.1.1 // indirect
   golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
   google.golang.org/appengine v1.6.5 // indirect
)

Unfortunately, you can't avoid them. Indirect dependency, is basically dependency that wasn't listed in go.mod of your direct dependency, but is still required by it.

In your case it happens, because you use github.com/gocolly/colly v1.2.0 as dependency and v1.2.0 of this package isn't a module, because it doesn't contain go.mod , so all it's dependencies are indirect and listed in your go.mod with indirect tag.

Note, that colly has go.mod in >= v2.0.0 , so if you require that version, these dependencies won't be listed as indirect in your go.mod .

go mod tidy helped me remove the indirect from go mod for me.

go mod tidy ensures that the go. mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module's packages and dependencies, if there are some not used dependencies go mod tidy will remove those from go.

There is an update since the answer was accepted. https://go.dev/ref/mod

At go 1.17 and above, the go command adds an indirect requirement for each module that provides any package imported (even indirectly) by a package or test in the main module or passed as an argument to go get.

Therefore, all dependencies are listed. This allows performance improvements in go tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM