How to download all the dependencies of fabric-sdk-go? - 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.

Related

Use go library that does not have go.mod

I am new to go and have trouble using a library that does not have a go.mod file. The library is https://github.com/yourbasic/graph and I tried installing it according to the instructions given in the go docs. Since I cannot make a request for the version on the repo, I used the #latest flag, so in order to install, I executed
go install github.com/yourbasic/graph#latest
This however fails and says
package github.com/yourbasic/graph is not a main package
Is there a proper way to install libraries like that? I assume my way of proceeding to just copy the files into a directory within my project is not very clean.
As #JimB said: instead of attempting to install it,
Import the package in your code where applicable.
Run go get github.com/yourbasic/graph#latest — which will download the module (to the local cache) and update your module's go.mod file.
Build.
The steps 1 and 2 can be swapped, but then the generated go.mod entry will have the // indirect comment which will disappear next time you run go mod tidy.

Understanding go mod and cause of package is not in GOROOT

I'm trying to play around with the lightning network. I have cloned the repo, and on disk placed it here (I'm using windows):
C:\Users\hallibut\Documents\GitHub\lnd
I'd like to run any of the tests in itest, lets say testMultiHopPayments. The cli commands I'm using after I cd into the above location is:
go test itest -run testMultiHopPayments
However, I keep getting the error:
package itest is not in GOROOT (C:\Program Files\Go\src\itest)
I've read through the various posts on this error, but I'm still not quite sure why it happens, and it's likely because I don't fully understand the go module (I'm new to go). This article, was probably the best in helping me understand the structure and env variables:
https://golangbyexample.com/workspace-hello-world-golang/
My understanding from the various readings is that whatever directory the go.mod file is in, indicates the module level directory. Prior to version 1.13 there was a required directory and structure, but now that should not be an issue if you're using at least version 1.13 and modules. I'm using 1.17.1. This is somewhat of an assumption or inference, but I believe everything lower in the directory structure is part of a package to be installed as part of the module (and is defined by the package keyword). However, I don't understand why a package with source code within a subdirectory would be missing/throw the aforementioned error. I've also tried running:
go mod install github.com/lightningnetwork/lnd/lntest/itest
That doesn't seem to do anything/has not effect on the error. What am I not understanding about packages? Looking at the go.mod file I observe that itest is not specifically defined anywhere… Not sure if that's required. Also, I assume I've got to run some build process prior? I attempted this with:
go install -v ./...
If you're using VS Code and Go Modules, you need to "Open folder" and point to the cloned repo, to get around that error

Get missing imports in batches

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

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