cannot find package "github.com/gorrila/mux" in any of - go

The error message is:
app.go:9:3: cannot find package "github.com/gorrila/mux" in any of:
/usr/local/Cellar/go/1.10.3/libexec/src/github.com/gorrila/mux (from
$GOROOT)
/Users/myname/go/src/github.com/gorrila/mux (from $GOPATH)
I understand GOROOT is for compiler tools that come with installation, so I am not sure why it looks for mux there. But I do see mux in the second location in my directory I created for go code.
I know this question was asked once before and I tried to debug following the advice from that question.
I used homebrew and installed go version go1.10.3 darwin/amd64.
Here is what I believe to be the relevant portion of my go env:
GOPATH="/Users/myname/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.3/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.3/libexec/pkg/tool/darwin_amd64"
I also set my bash profile, excluding GOROOT because that is no longer required:
export GOPATH="/Users/myname/go/src/github.com"
export PATH="/Users/myname/go/src/github.com/bin:$PATH"
And did source ~/.bash_profile.
This setup was enough that I can run basic go programs like hello world. So I then tried to run code using the mux library.
I first installed mux within the directory of my program (cd'd into /Users/myname/go/src/github.com/myname/restapi).
Then I ran:
go get -u github.com/gorilla/mux
I can see the folder exists in my finder. I also looked in terminal:
ls -l /Users/myname/go/src/github.com | grep gorilla
=> drwxr-xr-x 3 myname staff 102 Jun 29 14:35 gorilla
Then:
cd $GOPATH (/Users/myname/go)
go list ... | grep gorilla
=> can't load package: package ../..: no Go files in /Users
So I saw gorilla in the first command but not the second. However, I do see the gorilla directory further within my go directory, so I'm not sure what the issue is.
Running go build returns the package of concern:
app.go:9:3: cannot find package "github.com/gorrila/mux" in any of:
/usr/local/Cellar/go/1.10.3/libexec/src/github.com/gorrila/mux
(from $GOROOT)
/Users/myname/go/src/github.com/gorrila/mux (from $GOPATH)
I am not sure why it is looking for mux within the usr directory. Is there an issue with any of my paths? I keep checking that last path to see that the mux directory is there.

It looks like you misspelled "github.com/gorilla/mux" – it has one "r" and two "l"'s. (Carefully compare the go get command you quoted and the import statement in your source file.)

Can you try to set your .bash_profile in this way:
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

Related

Can not import modules in vscode

This is what the vscode is warning
could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of
/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
And these are the only two folders under go which I find out through my terminal:
Running go get -u github.com/gorilla/mux alone doesn't work.
I found a hacky way to fix the problem.
update your GOMODCACHE env go env -w GOMODCACHE=$GOPATH/src/src
run go get -u github.com/gorilla/mux
rename the directory mux#version to mux mv mux\#v1.8.0/ mux
terminate and restart vs code. The error will go away and you will be able to use intelliSense
If there is a better way let me know.
Try running the following command in terminal.
go get -u github.com/gorilla/mux
This should create src folder for you

how do you view .go files from imported packages you've installed?

Right now, I have imported a package. In my case it is
import "crypto/ecdsa"
This package contains a file called ecdsa.go. I'd like to view it, however I don't know where to see it. I can't seem to find it in my working directory. Is the file hidden? Or is it installed globally by default?
This package is a part of the Go project. You can find it in your <go-root-path>/src directory.
You can find your "go root path" by running go env GOROOT.
Probable location of Go in linux is /usr/local/go
Go standard library packages are located in GOROOT/src.
For GOROOT, run
go env GOROOT
For example, for package import "crypto/ecdsa" file ecdsa.go on Linux,
$ go env GOROOT
/home/peter/go
$ ls /home/peter/go/src/crypto/ecdsa/ecdsa.go
/home/peter/go/src/crypto/ecdsa/ecdsa.go
$

Cannot build "chaintool/example02"

I am trying to follow instructions on Chaincode Development Environment to setup hyperledger in my local environment.
Unfortunately, I am completely new to golang. When I come across an error trying to build "chaintool/example02" I have no idea how to proceed further - should I ignore the issue or first I should fix something? For example, to run make with some options, etc ... How can I get missing imports ?
The output looks as follows:
hyper-00:chaincode hyper$ pwd
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/examples/chaincode/chaintool/example02/src/chaincode
hyper-00:chaincode hyper$ go build ./
chaincode_example02.go:24:2: cannot find package "hyperledger/cci/appinit" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/cci/appinit (vendor tree)
/usr/local/go/src/hyperledger/cci/appinit (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/cci/appinit (from $GOPATH)
chaincode_example02.go:25:2: cannot find package "hyperledger/cci/org/hyperledger/chaincode/example02" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/cci/org/hyperledger/chaincode/example02 (vendor tree)
/usr/local/go/src/hyperledger/cci/org/hyperledger/chaincode/example02 (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/cci/org/hyperledger/chaincode/example02 (from $GOPATH)
chaincode_example02.go:26:2: cannot find package "hyperledger/ccs" in any of:
/Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/vendor/hyperledger/ccs (vendor tree)
/usr/local/go/src/hyperledger/ccs (from $GOROOT)
/Users/hyper/Projects/blockchain/src/hyperledger/ccs (from $GOPATH)
hyper-00:chaincode hyper$
My $GOPATH gives:
hyper-00:~ hyper$ echo $GOPATH
/Users/hyper/Projects/blockchain
[EDITED on 2017-01-02]
I repeated the repo cloning procedure again.
I think my mistake was I pulled a wrong source from GitHub - probably "master", not "v0.6".
What I did and the compilation works now is:
$ sudo apt install golang-go
$ gedit .profile
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/Projects/blockchain
$ . ~/.profile
$ mkdir -p $GOPATH/src/github.com/hyperledger/
$ cd $GOPATH/src/github.com/hyperledger
$ git clone -b v0.6 http://gerrit.hyperledger.org/r/fabric
$ cd ~/Projects/blockchain/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
$ go build
github.com/hyperledger/fabric should have included all these dependencies in its vendor/ folder or put the examples in their own repo with a vendor folder, so this looks like their problem not yours.
You can fix it by running go get x y z for all missing packages, or try using a vendoring tool to get them.
There are many options for vendoring but no "official" option until next year. I prefer govendor. i havent tried it with this particular repo but you can try with:
go get -u github.com/kardianos/govendor
cd /Users/hyper/Projects/blockchain/src/github.com/hyperledger/fabric/
govendor fetch -v +missing
The HyperLedger team has provided very good documentation on how to use chaintool in a manner that allows users to document APIs (via Chain Code Interface, or CCI). Without this, consumers of a chaincode must inspect source code to understand how to compose a REST invocation. Great idea.
This approach to building chaincode requires you download github.com/hyperledger/cci and github.com/hyperledger/ccs to reside next to github.com/hyperledger/fabric within your $GOPATH. Unfortunately, I don't see any repository (on either github or gerrit) where this can be downloaded.
Further, I've only seen one example (thus far) of how to use chaintool with cci and ccs, which is chaintool/example02... and it does not compile due to these missing hyperledger packages.
There is documentation on chaintool with cci/ccs support at the following location:
https://github.com/hyperledger/fabric-chaintool
But it seems this documentation has been copied to the following location and renamed from 'chaintool' to 'openblockchain compiler' (or OBCC) and labeled as a 'work in progress':
https://libraries.io/github/hyperledger/fabric-chaintool
As such, I am inclined to believe the chaintool/example02 is not a currently supported approach to building chaincode... at least until OBCC becomes officially available.

How do I install requirements in Go? "cannot find package"

I'm new to Go and I'm trying to set up a Go project with minimal documentation: https://github.com/alphagov/metadata-api
I've cloned it, but when I try go build I get the following warnings:
main.go:8:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus (from $GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus (from $GOPATH)
main.go:14:2: cannot find package "github.com/alphagov/metadata-api/content_api" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api (from $GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api (from $GOPATH)
I'm guessing this is because I haven't installed the Go equivalent of requirements?
My GOPATH is set:
metadata-api$ echo $GOPATH
/Users/me/go
And the Go executable is in
metadata-ape$ echo $PATH
....:/Users/me/go/bin
What do I need to do to help Go find these packages?
You should install package first:
try
$ go get github.com/Sirupsen/logrus
and check you $GOPATH dir
This project use gom as the package manager,
Make sure you have installed gom
or try this command
$ gom install
I think your $GOPATH and $PATH settings are incorrect, the $GOPATH environment variable specifies the location of your workspace, these are my path settings:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
I had similar issue and
export GO111MODULE=on
helped.
When you need your code to do something that might have been implemented by someone else (in Github or a package somewhere else), You should initialize a go mod file inside of your folder.)
For the purposes of this example, I'll just use example.com/module.
go mod init example.com/module
Add new module requirements and sums:
go mod tidy
Run your program:
go run .
For more details, see https://golang.org/doc/tutorial/getting-started.
Was able to fix the similar issue in Go 1.13.7 by typing:
export GOPATH=~/go
go get github.com/profile/repository
(e.g. github.com/Sirupsen/logrus)
"...Starting in Go 1.13, module mode will be the default for all development..."
"...When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin)..."

Using Go 1.2 cover tool with GVM

I'm using GVM to manage my go installations and paths and everything seems to work just fine - I can run tests and produce builds. I'm now trying to produce a code coverage file and am having some difficulty.
My package is defined in /home/bill/dev/server/model.
When I run:
$ go test -cover -coverprofile cover.out
The tests run successfully and a coverage file is produced. However, the paths in the coverage file look like this:
_/home/bill/dev/server/model/activity.go:19.34,21.2 1 1
And I get the following error when I try to create an html cover file:
$ go tool cover -html=cover.out
cover: can't find "activity.go": cannot find package "_/home/bill/dev/server/model/" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/_/home/bill/dev/server/model (from $GOROOT)
/home/bill/.gvm/pkgsets/go1.2/global/src/_/home/bill/dev/server/model (from $GOPATH)
How do I fix this?
Additional details
~ gvm use go1.2
Now using version go1.2
~ echo $GOPATH
/home/bill/.gvm/pkgsets/go1.2/global
~ echo $GOROOT
/home/bill/.gvm/gos/go1.2
I tried manually setting my $GOPATH but that didn't change the cover.out file. I also tried manually editing the cover.out file but I can't figure out what paths it actually wants. In the default configuration shown above, running go test runs as expected.
Attempting to fix GOPATH
~ export GOPATH=/home/bill/dev/
~ ln -s /home/bill/dev/server /home/bill/.gvm/gos/go1.2/src
~ go test
cannot find package "code.google.com/p/go.crypto/pbkdf2" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/code.google.com/p/go.crypto/pbkdf2 (from $GOROOT)
/home/bill/dev/src/code.google.com/p/go.crypto/pbkdf2 (from $GOPATH)
../util/log.go:4:2: cannot find package "github.com/kr/pretty" in any of:
/home/bill/.gvm/gos/go1.2/src/pkg/github.com/kr/pretty (from $GOROOT)
/home/bill/dev/src/github.com/kr/pretty (from $GOPATH)
These are additional dependencies that I previously downloaded using go get. They end up in /home/bill/.gvm/pkgsets/go1.2/global/src which the $GOPATH used to point to. So I changed GOPATH
~ export GOPATH=/home/bill/dev/:/home/bill/.gvm/pkgsets/go1.2/global
So that the tests run again, but the cover.out file still has the same directories in it and still gives me the same error.
Here's the way to get all of the advantages of GVM without having to ruin your ideal go development environment as described here, and without having to resort to clunky special-case symlink hacks.
Suppose I've set all of my development up according to the standard in ~/go (so package foo in my github would be in ~/go/github.com/gepoch/foo)
First of all, we're going to make a special-use pkgset that will happily reference our development environment. Simply run:
$ gvm pkgset create dev
This will add the pkgset. Next, we can do some customization on where exactly it puts the go path. Run:
$ gvm pkgenv dev
You should see your favorite text editor pop open with a bunch of environment variable definitions. Simply change the GOPATH entry to include your dev root! For example, I change this:
export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev"
Into this:
export GOPATH; GOPATH="$GVM_ROOT/pkgsets/go1.2/dev:$HOME/go"
Additionally, have gvm set up your path correctly by changing this:
export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"
into this:
export PATH; PATH="${GVM_ROOT}/pkgsets/go1.2/global/bin:${GVM_ROOT}/gos/go1.2/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}:$HOME/go/bin"
Restart your terminal, and that's it! Whenever you run $ gvm pkgset use dev you'll have easy access to your dev environment.
That means (among many other things) that this works as intended:
$ go test -coverprofile=coverage.out github.com/gepoch/foo
$ go tool cover -html=coverage.out
You can add this to any pkgset environment that you wish, for easy access to the dev tree.
I had the same problem a month ago. I solved it by using the following steps.
My package name is called alpaca
My working directory (code) is /home/pksunkara/coding/alpaca
$ gvm use go1.2
Now using version go1.2
$ echo $GOPATH
/usr/local/lib/gvm/pkgsets/go1.2/global
$ echo $GOROOT
/usr/local/lib/gvm/gos/go1.2
To fix the issue, I did this
$ mkdir -p $GOPATH/src/github.com/pksunkara
$ ln -s /home/pksunkara/coding/alpaca $GOPATH/src/github.com/pksunkara/alpaca
Basically I have to link the current working folder into the $GOPATH/src folder and the resultant package path for alpaca became github.com/pksunkara/alpaca.
Now, the go test & cover works as following
$ go test -coverprofile=coverage.out github.com/pksunkara/alpaca
$ go tool cover -html=coverage.out
THIS IS IMPORTANT
I stumbled a lot to fix this. I have attempted all kind of things including the ones you attempted. I understood the problem by reading about code organization in golang which should be a must read for everyone working with go.
The code organization mentioned here is very important to work with golang.
Package paths are important for golang. And you should never use local path when importing in golang. They will work but it is not recommended.
Let's assume your package name is model. You can simply link the model directory to $GOPATH/src/model and then you will have a package path named model which you can import using import "model". But to avoid collisions, go recommends using a bigger package path name.
I would recommend you to link it to $GOPATH/src/bill.com/server/model and import it as import "bill.com/server/model". Similarily with ./query and ./util you have.
If you still have doubts, please ask. I will try to explain more.
Have you try to put a issue in gvm's developer site? https://github.com/moovweb/gvm (I'm not sure is this the major site)
Double check the value of $GOPATH, as set by gvm.
I would try setting $GOPATH manually just for testing to /home/bill (with a symlink src->dev), just to see if a go test -cover produces files a cover.out with the correct file path in it.

Resources