Get missing imports in batches - go

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.

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 ./...

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

Related

golang check package dependencies on a single repository

I am working on a golang project which have a few packages (folders). My purpose is to pull out a certain package into a separate module, but before I do this I want to see the package dependencies. Is there any tool out there which I can run to view the package dependencies. I am not talking about 3rd party modules but just simple folders in the same repo. I checked around for a few tools but could not find any.
go list -f {{.Deps}} path/to/your/package gives you all direct and indirect dependencies.
For Go modules you can do go mod graph or go list -m all.

How to download all the dependencies of fabric-sdk-go?

I used command "go get github.com/hyperledger/fabric-sdk-go" to download fabric-sdk-go and its dependencies. No error happened.
In the golang documentation( https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies ), it said that "Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'."
So I originally thought that all the dependencies of fabric-sdk-go would be downloaded recursively. But the fact proved that I was wrong.
When I ran command "go install ./..." under fabric-sdk-go directory, many errors "cannot find package" were displayed:
So my questions are:
Does "go get" download dependencies recursively or not?
How to download all the dependencies of fabric-sdk-go, instead of using "go get [a_dependency_package]" to download every single dependency one by one?
Thanks very much.
There is no entry point in the root of the project (i.e. no main method) so there is nowhere for the tool to start looking as it doesn't actually do recursive downloads. Instead it looks at the files in the directory you named in the URL and fetches import paths named in those files. For future reference this command will do what you want it to, go get github.com/hyperledger/fabric-sdk-go/... you can append the triple dot right to your go get command
Does "go get" download dependencies recursively or not?
Yes it does. No need to worry or doubt the documentation
How to download all the dependencies of fabric-sdk-go, instead of using "go get [a_dependency_package]" to download every single dependency one by one?
Just use go modules: export GO111MODULE=on and build your code.

"go get -t ./...", Travis CI, and installing all go dependencies

In the documentation here: https://docs.travis-ci.com/user/languages/go#Dependency-Management
This is the install dependencies step:
if go version is greater than or equal to 1.2
go get -t ./...
My project looks like this:
root
---- src
---- github.com
---- myProject
---- pkg
the GOPATH is set to root
but I am getting this error:
package github.com/aws/aws-lambda-go/lambda: home/travis/build/path/to/package exists but home/travis/build/path/to/package.git does not - stale checkout?
How do I get all the dependencies to install at once like an npm install in nodejs?
How do I get all the dependencies to install at once?
That's what go get does... it looks like something might be misconfigured or corrupted in your case. You could start by trying some things.
go get installs into the first path in the $GOPATH environment variable. Confirm that it is set to what you want (usually a path ending in a directory named go; subdirs src, pkg, etc. will be created).
If you're going to use ./..., make sure you're calling go get from the right directory.
Check to make sure the git root is in the right place
try using a non-wildcard name/path/address for the package you want to get, instead of ./...
try calling go get without the -t flag
If none of that works, you may be able to solve the problem by deleting the directory (home/travis/build/path/to/package) and trying again -- make sure you aren't deleting any code you worked on, or the git repository/files, unless it's backed up somewhere.
According to Go tools documentation, you should only need to call
go get [packages]
to install the named packages along with their dependencies:
Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.
The ... ellipsis is a wildcard that can expand to match any string. See section on Description of Package Lists:
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.
The ./ means "here": so make sure you're running from the right directory if you're going to use ./...
The -t flag is for downloading the packages required to build the tests:
The -t flag instructs get to also download the packages required to build the tests for the specified packages.
The error you're seeing is related to git. Sometimes the cause is unknown, but it can often be fixed by deleting the directory and starting again (see, e.g., "Error to install golint" or "Correct way to get package")
(You might also find this blog post on configuring travis-ci for Go helpful.)

can't load package: package .: no buildable Go source files

Here is the error message:
% go get
can't load package: package .: no buildable Go source files in /Users/7yan00
% echo $GOPATH
/Users/7yan00/Golang
How would you troubleshoot that error?
Make sure you are using that command in the Go project source folder (like /Users/7yan00/Golang/src/myProject).
One alternative (similar to this bug) is to use the -d option (see go get command)
go get -d
The -d flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages.
See if that helps in your case.
But more generally, as described in this thread:
go get is for package(s), not for repositories.
so if you want a specific package, say, go.text/encoding, then use
go get code.google.com/p/go.text/encoding
if you want all packages in that repository, use ... to signify that:
go get code.google.com/p/go.text/...
You should check the $GOPATH directory. If there is an empty directory of the package name, go get doesn't download the package from the repository.
For example, If I want to get the github.com/googollee/go-socket.io package from it's github repository, and there is already an empty directory github.com/googollee/go-socket.io in the $GOPATH, go get doesn't download the package and then complains that there is no buildable Go source file in the directory. Delete any empty directory first of all.
Another possible reason for the message:
can't load package: .... : no buildable Go source files
Is when the source files being compiled have:
// +build ignore
In which case the files are ignored and not buildable as requested.This behaviour is documented at https://golang.org/pkg/go/build/
To resolve this for my situation:
I had to specify a more specific sub-package to install.
Wrong:
go get github.com/garyburd/redigo
Correct:
go get github.com/garyburd/redigo/redis
If you want all packages in that repository, use ... to signify that, like:
go get code.google.com/p/go.text/...
you can try to download packages from mod
go get -v all
I had this exact error code and after checking my repository discovered that there were no go files but actually just more directories. So it was more of a red herring than an error for me.
I would recommend doing
go env
and making sure that everything is as it should be, check your environment variables in your OS and check to make sure your shell (bash or w/e ) isn't compromising it via something like a .bash_profile or .bashrc file. good luck.

What is the idea behind Go package naming convention?

I'm trying to understand the idea behind package naming convention in Go. Most packages are installed and imported as something like:
import "github.com/howeyc/fsnotify"
I get the idea that package names should be unique, but I don't see the point of using the website github.com. Why not just use author/package? Like:
import "howeyc/fsnotify"
That's not likely to ever collide. Or some other "shorter" strategy? Is it because it "just works" with go get? Or is there some other reason?
You can use howeyc/fsnotify if you want to. When github.com/howeyc/fsnotify is used it's understood that the package is hosted on Github. Other repositories work as well.
The reason is it makes it easier to locate and install dependencies with go get. Otherwise you'd have to satisfy the dependencies manually. And since forking repos is quite common in the open-source world, you may have a modified version from the same author. So it helps to distinguish what your project depends on.
Download and install packages and dependencies
Usage:
go get [-d] [-fix] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
When checking out or updating a package, get looks for a branch or
tag that matches the locally installed version of Go. The most
important rule is that if the local installation is running version
"go1", get searches for a branch or tag named "go1". If no such
version exists it retrieves the most recent version of the package.
For more about specifying packages, see 'go help packages'.
For more about how 'go get' finds source code to download, see 'go help remote'.
The import path supports the go get command. Paths denoting remote repositories begin with the path to the code. Run the go help remote command for details.

Resources