Can not import modules in vscode - go

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

Related

go install #latest found but does not contain package

I'm trying to install my package using go install but I get this error message when running the command go install github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest:
go install: github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest: module github.com/JoaoDanielRufino/gcloc#latest found (v1.0.0), but does not contain package github.com/JoaoDanielRufino/gcloc/cmd/gcloc
I want the executable name to be gcloc.
Here is the current source code: https://github.com/JoaoDanielRufino/gcloc
Note: I've already tried go clean -modcache but it didn't work
As the main function of this package isn't on its root, you should pass the directory of the main package on your command.
So, your command will be:
go install -v github.com/JoaoDanielRufino/gcloc/cmd#latest
I came across a similar issue when I was trying to use go install to install the cloudflare/cf-terraforming tool on my machine. The documentation for this tool is not clear on the installation and I had to dig around to get this to work
Basically #Jictyvoo answer above sums it up, if the path is pointing to anything other than directory where the main.go file is sitting I got the error
Command: go install github.com/cloudflare/cf-terraforming#latest v0.8.0#latest
go: github.com/cloudflare/cf-terraforming#latest: module
github.com/cloudflare/cf-terraforming#latest found (v0.8.0), but does not
contain package github.com/cloudflare/cf-terraforming
when I switched to the below it worked fine for me:
Command: go install -v github.com/cloudflare/cf-terraforming/cmd/cf-terraforming#latest
This worked for me after checking the repo and realising that the main.go file was sitting in the cmd/cf-terraforming subdirectory

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"

Why cannot import library from go get?

I downloaded go-redis client using
go mod init github.com/my/repo
go get github.com/go-redis/redis/v8
But it showed cannot find package "go.opentelemetry.io/otel/api/trace". So I deleted go-redis from ${GOPATH}/src/github.com and then tried running it again
go get github.com/go-redis/redis/v8
But it does nothing. Doesn't show any error or any message. But when I try to import package it says
cannot find package "github.com/go-redis/redis/v8" in any of:
 /usr/lib/go/src/github.com/go-redis/redis/v8 (from $GOROOT)
 /home/username/go/src/github.com/go-redis/redis/v8 (from $GOPATH)
I tried go mod tidy go mod clean but none worked. What should I do?
ls $GOPATH , if is not showing your path of golang library source, you should set path first,
If point 1 is able, you should just doing :go mod tidy
tidy argument make you doing download package with sync method, without doing go get separately.
I would rather use go vendoring, it will add your dependencies to vendor/ and -mod=vendor will tell golang to search for dependencies locally.
Remove go.sum
Type export GOFLAGS=-mod=vendor
Type go mod tidy && go mod vendor
If you can't export variables, use go run and go build with the prefix GOFLAGS=-mod=vendor, for example GOFLAGS=-mod=vendor go run cmd/main/main.go
Don't forget to add vendor/ to your .gitignore

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

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

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

Resources