go: module found but does not contain package - go

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

Related

go install apache arrow

I'm a beginning go user trying to install the go apache arrow module, so I can run the introductory examples in the user guide. When I try to install the library, I receive the following errors:
$ go install github.com/apache/arrow/go#latest
go: github.com/apache/arrow/go#latest:
module github.com/apache/arrow#latest found (v0.0.0-20220326002331-5bd4d8ec279d),
but does not contain package github.com/apache/arrow/go
$ go install github.com/apache/arrow/go#v7.0.0
go: github.com/apache/arrow/go#v7.0.0: github.com/apache/arrow/go#v7.0.0:
invalid version: go/go.mod has post-v7 module path
"github.com/apache/arrow/go/v7" at revision go/v7.0.0
I've been able to install other go packages successfully, so I don't understand why this install is erroring out.
What is the correct invocation of "go install" to install apache arrow?
There is an easy way: assume you have done go mod init in your local project, you can start to write a file and import github.com/apache/arrow/go/v8 Like this example:
https://github.com/apache/arrow/blob/master/go/arrow/_examples/helloworld/main.go
Now you can easily do
go mod tidy && go mod vendor
And the tool should recognize the imports to download and vendorize.
Or you can do explicitly in your project dir,
go get -u github.com/apache/arrow/go/v8
then run the mod tidy and mod vendor

go 1.14 to 1.17 update -> modules not working anymore

After updating Go from 1.14 to 1.17 I'm getting this error:
main.go:10:2: no required module provides package github.com/gin-gonic/gin: \
go.mod file not found in current directory or any parent directory; \
see 'go help modules'
I used to be able to fix that with go get github.com/gin-gonic/gin but now that doesn't help. Did something change?
I can repro this if I have a file like this:
package hello
import _ "github.com/gin-gonic/gin"
and run these commands:
go mod init hello
go build
Fix for me is running this command:
go mod tidy
As of Go 1.16, “Module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory. More precisely, the GO111MODULE environment variable now defaults to on.”
See the Migrating to Go Modules blog post for a quick overview of how to migrate, or Tutorial: Create a Go Module for more detail.
According to documentation we must use go install example.com/cmd#latest instead of go get for go 1.17+

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

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

go module #latest found but does not contain package

I'm trying to make use of go module for the first time. What exactly the following error message is telling me?
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
It happens during go build, whereas go get is just fine:
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
but not go get -v ./..., which gave me the same error as above. My proxy setting looks OK:
$ go env | grep GOPROXY
GOPROXY="https://proxy.golang.org,direct"
Is it a problem of the go module/package I'm trying to use, or my own code's problem? -- I took a look at
https://github.com/mkideal/cli/blob/master/go.mod and it seems fine to me.
See the following update for details.
How can I overcome the situation? (I'm getting the same error message for my own repo as well)
UPDATE:
Here is the full log how I'm getting the above error:
prepare /tmp/015-file from https://github.com/mkideal/cli/blob/master/_examples/015-file
do go mod init
then go build
Now the details:
$ cd /tmp/015-file
$ GO111MODULE=on
$ go mod init github.com/mkideal/cli/015-file
go: creating new go.mod: module github.com/mkideal/cli/015-file
$ cat go.mod
module github.com/mkideal/cli/015-file
go 1.14
$ go build
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
$ go get -v ./...
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go version
go version go1.14.1 linux/amd64
Try clearing cache:
go clean -modcache
For more info on how this command works, use go help clean
In my case cleaning cache didn't help.
Running go install in a project root printed no Go files in ... and that was the root cause, in the same time running go install gitlab.com/.... printed info about a missing package.
What had to be done was creating a go file in a project root directory with main function.
Update to go version go1.14.3 linux/amd64
Clear go module cache
don't know which one solved the problem (or both), now AOK.
I had the same error, but in my case I was attempting to import a module that made available only resource files, and no go pkgs. Adding an empty go file in the module with a package declaration solved it.
In my case, go.mod files were under src, after moving the go.mod file into one level up, then it works
Refer the Samples below,
directory structure when "package not found" error
dir1/src/
main.go
go.mod
go.sum
directory structure after fix
dir1/
go.mod
go.sum
src/
main.go
I had a similar problem. In my case the package name was not matching the name of the folder it resided in.

Resources