cannot find package google.golang.org/grpc - go

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"

Related

go: module found but does not contain package

I am trying to install the net package for go but get "does not contain package error".
Terminal screenshot:
I have consulted: go module #latest found but does not contain package but none of the solutions seem to work for me.
I am using go version go1.18.5 linux/amd64
You have to initialize your module with go mod init in the project root directory
For local codebase
go mod init test
OR for hosted codebase e.g. github repo: test, github user: radiant
go mod init github.com/radiant/test
It will produce a go.mod file.
Then you can get the required package as:
go get golang.org/x/net
go mod tidy
Then import and use the net packages.
Hope this helps.
go get -u golang.org/x/net
go install used for executable packages

golang offline install third party package

I'm trying to install third party package of golang in offline.
I want to do something like go get in an online environment in an offline environment. I can get the source of the third party package from github and put it in an offline environment, but then I can't use it as a package in golang.
What should i do?
As #emptyhua commented, this link worked for me: https://gist.github.com/gmolveau/f09c1038ca622620e54d0579ba06ea96#file-golang_offline-md
For reference here:
On the online machine ensure go.mod and go.sum exist in current dir
create a go file with:
import (
_ "github.com/gorilla/mux"
_ "github.com/sirupsen/logrus"
)
func main() {}
run: go mod vendor
Copy go.mod, go.sum and vendor directory to the offline machine
run: go run -mod=vendor main.go
To test offline, I used a container with no network:
podman run --rm -it --net none -v $(pwd):/go/src:z golang
cd src
go run -mod=vendor main.go
Hope that helps! Thanks #gmolveau - offline go mods
First of all, you have to set the GOPATH for the working directory.
After then you can use the following offline package just you have to create a mod file for the path of the packages.
Initialize go mod file using below code
go mod init <Enter your GitHub profile link>
For example:
go mod init github.com/amshashankk
after this when you import the packages they will be written as following in the import section of the code
github.com/amshashankk/package.

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

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

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