ide does not find packages shim and proto packages in $GOPATH - go

I have been setting up a small Hyperledger fabric application. It is already running and I can add delete and change users.
But until now I have been using nano to code until now.
I want to change to a proper ide (goland) for the sake of autocompletion and so on.
The problem is: On my local machine it can not find the packages
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
The gopath is set up correctly but the two packages are not in the folders. I.e also a local go build does not work.
The packages are present on the CLI though.
How can I install the dependencies so that I also have them on my local machine? And is this even desired and if so why not?
ps: I have already tried
go get -u github.com/hyperledger/fabric/core/chaincode/shim
the resulting error is:
package github.com/hyperledger/fabric/core/chaincode/shim: cannot find package "github.com/hyperledger/fabric/core/chaincode/shim" in any of:
/usr/local/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOROOT)
/home/funuser/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOPATH)

The issue is, the shim and protos were moved to stand-alone repositories several months ago, you need to do a go get on github.com/hyperledger/fabric-chaincode-go/shim and github.com/hyperledger/fabric-protos-go and change your import references to these new repos:
import (
"fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)

Unfortunately, go get only imports go code files. Other assets like the raw proto files are excluded. An option is to check-out the git repo in $GOPATH/src/github.com/hyperledger/fabric

this worked for me https://www.programmersought.com/article/17446289383/
additional any one using this thread now need to update the pkgs to
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"

Related

Cannot find package when running golang test

When I run go tests I get the error:
cannot find package "github.com/stretchr/testify/assert" in any of:
/usr/local/Cellar/go/1.17.6/libexec/src/github.com/stretchr/testify/assert (from $GOROOT)
/Users/MyName/go/src/github.com/stretchr/testify/assert (from $GOPATH)
I do however see that my local installation of stretchr and other tools ARE in /Users/MyName/golang/pkg/mod/github.com
How do I get golang to look in the right place for these installations? I thought it would be grabbing the resource from github if the import is pointing to the repo.
Are my installations in the "wrong" spot, and should I move them to where golang would look for them by default?
I have been able to test using stretchr before, but upgrading the package didn't change anything.
Running go get github.com/stretchr/testify and go mod tidy also didn't seem to change the behavior.
Thanks in advance for your help.
Run "go mod init" whilst in your folder with your code, then go mod tidy.
It should work, if not, try go get ... then try this again.
Some IDE's such as VSCode delete imports when saving if they're unrecognised, or not used, so make sure when you run go mod init, and tidy, that the import is actually in the file
I closed VS Code and re-opened, and it's working now...

Is it possible to update local packages without running go install?

I am trying to import a local file into my main.go file and this tutorial (and other similar tutorials) says to run go install <path> in order to import that path as a package. This seems like a slow way to develop local packages because you would have to run go install <path> every time you want to see the changes in your local package.
Is there a faster way to import/update local packages? I am using gomon to auto-reload my code after updating it, so ideally, my code would auto-reload after updating a local package.
You should use go modules. The tutorial you mentioned appears to be older than the modules feature. In short: you can import a package, run go build, and any imported external package will automatically be downloaded for you as needed, no need to do a go get. Start here:
https://blog.golang.org/using-go-modules
https://github.com/golang/go/wiki/Modules

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

Go vendoring outside $GOPATH

I have a project which is built in Node.js/Express.js. I want to start to rewrite this to go/iris framework. I don't want to re-factor everything into my $GOPATH and I want to keep it together my express / go / docker files for this project.
I tried to clone iris framework's git repo into a ./vendor subfolder, but using import "github.com/kataras/iris" importing nothing.
Is there a package manager which is
copying and installing packages and all of it's dependencies in my-project/vendor folder outside $GOPATH
it can update these import packages
go run/build/install outside $GOPATH
there's now any new files in $GOPATH src/pkg/bin folder when I working on a project, except this package manager
I can define dependency packages for a project like package.json file for node.js
Is there a go package manager like that?
Edit:
Running this with go command is not required.
Not possible with the current go tooling, but looks like we might get it in go 1.12 or bit later.
Proposal accepted:
cmd/go: modify the Go toolchain to work without GOPATH

Cannot install docker pkg dependency in Go

I'm trying to use the docker package in one of my Go applications. I'm importing the package as import "github.com/dotcloud/docker" in my script. But when trying to build the dependencies, that is, when I run go get in my project directory, it says:
foo.go:9:2: no buildable Go source files in /home/neville/gocode/src/github.com/dotcloud/docker
Here, my GOPATH is set to /home/neville/gocode, so when doing go get, the package should get downloaded to /home/neville/gocode/pkg, instead of /home/neville/gocode/src. What am I missing here?
github.com/dotcloud/docker isn't a Go package, and that's why there are no source files in that directory.
Import the package you want directly, like so for the registry package:
import "github.com/dotcloud/docker/registry"
Also, go get does download into $GOPATH/src. The installed object files go in $GOPATH/pkg.

Resources