批量获取丢失的进口

When I clone a project written in golang, it is normal that a lot of imports like 'github.com/XXXX' are missing. Is there any way to get these imports in batches by a command? or I am suppose to get them one by one.

A lot of golang projects now use dependency management so you should look for that first. e.g a Glide.lock (glide) or Gopkg.lock (dep - the way people are moving now) file present in the root of the project.

https://github.com/golang/dep

https://golang.github.io/dep

if dep is used and you have it installed then dep ensure will set the dependencies up for you and make sure you get the versions the author intended

if a project is not using dependency management you can just get the packages with go get ./... but I don't think you will be guaranteed the correct versions (e.g if the author was pinned to a version tag for a dep)

If you run dep init it sets up dep on a project and will attempt to resolve the correct versions, however this doesnt always work if the stars dont align (e.g I have seen issues with dependencies using gopkg.in)

try using go get ./... in root of your project

You should use go get to get "remote" packages. Quoting from Command go: Download and install packages and dependencies

Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.

You may use the -v flag in all of the following commands, and so you will see what go get is doing under the hood.

You may use the -d flag if you just want to download the packages but you do not want to install them.

The examples use the example remote package github.com/somebody/somepackage, but obviously it works for other packages hosted outside of github.com.

For more information, see the official doc: Command go, or type go help get.


To get a single package with all the dependencies of that package and install them, use

go get github.com/somebody/somepackage

To get a package with all its dependencies, and all other packages rooted at that path (along with their dependencies), and install all of them, use:

go get github.com/somebody/somepackage/...

Quoting from Command go:

An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.

To get a package with all its dependencies (and "subpackages") including dependencies of tests, and install all of them, use:

go get -t github.com/somebody/somepackage/...

To update a package you already have, use:

go get -u github.com/somebody/somepackage/...

To fetch dependencies of a package you already have (which is not necessarily from a remote location):

go get path/to/package/name/...

Or go to its folder and then you may use a relative path:

go get ./...