Why cannot import library from go get? - go

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

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

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"

Go mod use last version, not commit

I want to use library according to last commit not to last release. So I got that version:
$ go get github.com/epsagon/epsagon-go#636ea43
Now in packages cache I have:
$ cd /Users/sgetman/go/pkg/mod/github.com/epsagon && ls
epsagon-go#v1.14.0 epsagon-go#v1.14.1-0.20201105151128-636ea43d1943
But when I try go build, go test, go mod tidy:
go: finding module for package github.com/epsagon/epsagon-go/epsagon/wrappers/gin
github.com/nexmoinc/neru-runtimelib/router imports
github.com/epsagon/epsagon-go/epsagon/wrappers/gin: module github.com/epsagon/epsagon-go#latest found (v1.14.0), but does not contain package github.com/epsagon/epsagon-go/epsagon/wrappers/gin
At the same time my go.mod:
require (
...
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943
...
)
go.sum:
github.com/epsagon/epsagon-go v1.14.0 h1:Tq7qyoyDs2aUCc/UsQEHFt89aXVdUmjWXHwvS5kfSC4=
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943 h1:kJGvRsqRfo1h8vEEGajWa+szA9965Epw83Fm3UmmwEc=
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943/go.mod h1:Q73D3EhfzqmQa6m6Xi5n0Ugw9l6XSNGCzMcozsFMD1c=
Could you please help me to sort why go mod relys on the latest version, not version I provided?
The go command is checking the latest version because package github.com/nexmoinc/neru-runtimelib/router contains an import statement like import "github.com/epsagon/epsagon-go/epsagon/wrappers/gin".
github.com/epsagon/epsagon-go v1.14.1-0.20201105151128-636ea43d1943 does not contain such a package, so the go command is trying to figure out whether it can upgrade that module to a newer version in order to find the imported package.
If you run go build -mod=readonly, you will hopefully get a clearer error message. (Note that -mod=readonly will be the default as of Go 1.16: see https://tip.golang.org/doc/go1.16#modules.)
These steps worked for me although I do not know if they are the recommended ones
Comment out all instances of that import path in all files
remove go.sum
did go mod tidy
did go mod vendor
did go get -u -d
uncommented the lines in all files again
did go mod tidy again

How to remove an installed package using go modules

I've installed a package using go modules (go get in Go 1.13) and now I want to remove it. In the documentation there is nothing about this and in go get docu neither.
Removing the package from go.mod manually doesn't solve the issue so it remains in go.sum.
How should I remove a package in a clean way?
Found it https://go.dev/blog/using-go-modules#removing-unused-dependencies
go mod tidy
So basically, once the package is not being imported in any package you can perform a go mod tidy and it will safely remove the unused dependencies.
And if you are vendoring the dependencies, then run the command below to make the module changes be applied in the vendor folder:
go mod vendor
#jesugmz answer doesn't say, what if you wanna remove a currently using package in go modules.
So, if you're using go modules (you have a go.mod file in your project) and you want to remove a currently using package, check $GOPATH/pkg/mod/ directory and simply remove the package named package#version.
For example, if you have github.com/some/project package installed, you should run the following command:
rm -rf $(go env GOPATH)/pkg/mod/github.com/some/project#v1.0.0
You can find the using package version in go.mod file.

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