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

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)..."

Related

cannot find package google.golang.org/grpc

When I try to import the grpc package I get the following error:
could not import google.golang.org/grpc (cannot find package "google.golang.org/grpc" in any of
/usr/local/go/src/google.golang.org/grpc (from $GOROOT)
/home/ansh/Go/src/google.golang.org/grpc (from $GOPATH))
This is my gopath (incase it helps):
export GOPATH="$HOME/Go"
export PATH="$PATH:/usr/local/go/bin:$GOPATH/bin"
I did install these two packages:
$ go install google.golang.org/protobuf/cmd/protoc-gen-go#v1.26
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#v1.1
But it doesnt seem to work
In case you are using go modules, there is no need to set GOPATH anymore. However if GOPATH is set and you want to use a package from your project directory you need to set GO111MODULE=off, because by default GO111MODULE is set as ON. So even if the package google.golang.org/grpc is in your GOPATH you will have to force Go not to use the Go modules.
So something like this should work in case you are still opting of using GOPATH.
$ GO111MODULE=off && go install google.golang.org/protobuf/cmd/protoc-gen-go#v1.26
$ GO111MODULE=off && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#v1.1
If you haven't defined a go.mod file yet, please run the "go mod init ..." command to create it.
Then run the following command:
go get -u google.golang.org/grpc
in case grpc with updated gopath, dependencies will be looking gopath
Step1 : create go mod file in base folder of the project
"go mod init"
Step2 : run
"go mod tidy"
which loads the dependencies to "go.sum"

Error in compiling golang after migrating to centos

I was migrating my Golang programs from windows to Centos 7
It worked perfectly in Windows
but when I tried to compile on centos I get errors like
main.go:20:3: cannot find package "github.com/BurntSushi/toml" in any of:
/usr/local/go/src/github.com/BurntSushi/toml (from $GOROOT)
/root/work/src/github.com/BurntSushi/toml (from $GOPATH)
main.go:15:3: cannot find package "github.com/dgrijalva/jwt-go" in any of:
/usr/local/go/src/github.com/dgrijalva/jwt-go (from $GOROOT)
/root/work/src/github.com/dgrijalva/jwt-go (from $GOPATH)
main.go:16:3: cannot find package "github.com/gwlkm_service/config" in any of:
/usr/local/go/src/github.com/gwlkm_service/config (from $GOROOT)
/root/work/src/github.com/gwlkm_service/config (from $GOPATH)
kinda new to centos so idk what to do
looks like you have configured your GOPATH, without Go Module, you can use go get [package path] to download imported packages.
go get github.com/BurntSushi/toml
go get github.com/dgrijalva/jwt-go
go get github.com/gwlkm_service/config
Precisely
your go installation on server seems in /usr/local/go and
your project is in /root/work
so all your dependencies should be either in /root/work/src or /usr/local/go/src
now coming to action check your GOPATH with running echo $GOPATH
assuming it is automatically set to /usr/local/go/src
If not then follow - How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?
If everything is ok then in your folder run go mod init
this will create mod file which will help you in further installations
look into - https://blog.golang.org/using-go-modules
then run go get commands as above #beiping96 said
go get github.com/BurntSushi/toml
go get github.com/dgrijalva/jwt-go
go get github.com/gwlkm_service/config
NOTE - after completing above process you will generate go modules file(same as package.json) and in future you won't need to care about dependencies

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 find package "github.com/user../../" in golang while building

I am trying to build a project from github source code. I have found some source code importing a package something like below:
import (
"os"
"github.com/bivas/rivi/commands"
"github.com/mitchellh/cli"
)
However, while building a project every time it throws an error:
user-MacBook-Pro:rivi user$ go build rivi.go
rivi.go:6:2: cannot find package "github.com/bivas/rivi/commands" in any of:
/usr/local/Cellar/go/1.7.5/libexec/src/github.com/bivas/rivi/commands (from $GOROOT)
($GOPATH not set)
rivi.go:8:2: cannot find package "github.com/mitchellh/cli" in any of:
/usr/local/Cellar/go/1.7.5/libexec/src/github.com/mitchellh/cli (from $GOROOT)
($GOPATH not set)
how to build this project. Currently I am trying to build this one project into my system.
EDITED:
After running this command go install or go get:
package github.com/bivas/rivi/commands: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/connectors/github: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/autoassign: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/automerge: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/commenter: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/labeler: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/locker: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/sizing: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/slack: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/status: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/trigger: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/mitchellh/cli: cannot download, $GOPATH not set. For more details see: go help gopath
This question is a few years old now. I was experiencing the same problem. The solution for me was that I needed to run go mod init [name_of_main_go_file], which creates the go.mod file. Then you can run go run .
https://golang.org/doc/tutorial/getting-started#call
This problem common if you haven't setup any go path yet. It is necessary.
I am assuming from the command line snap of yours, that you are a UNIX user. Googles official doc recommend that you setup your go path manually . $GOROOT is optional but $GOPATH has to be set if you are trying to get third party libraries.
Edit your ~/.bash_profile to add the following line:
export GOPATH=$HOME/work
Then logout and login or use source ~/.bash_profile
You can read more from here. Assuming your Golang package directory is in work folder inside your home directory. The directory structure of work folder has to look like this,
work
src
pkg
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