How can you use go doc without modules? - go

Is it possible to use go doc for a 'downloaded' package without creation of a module?
~/tt$ ls # empty
~/tt$ go install go.mongodb.org/mongo-driver/bson#latest
package go.mongodb.org/mongo-driver/bson is not a main package
~/tt$ go doc go.mongodb.org/mongo-driver/bson
doc: no required module provides package go.mongodb.org/mongo-driver/bson: go.mod file not found in current directory or any parent directory; see 'go help modules'
exit status 1
If you create a module that includes it, it works.
~/tt$ go mod init testing
go: creating new go.mod: module testing
~/tt$ go get go.mongodb.org/mongo-driver/bson
go: added github.com/go-stack/stack v1.8.0
go: added go.mongodb.org/mongo-driver v1.9.1
~/tt$ go doc go.mongodb.org/mongo-driver/bson | head -2
package bson // import "go.mongodb.org/mongo-driver/bson"
In both cases, the bson package is located in /home/ubuntu/go/pkg/mod/go.mongodb.org/mongo-driver#v1.9.1/bson. This feels that stdlib is getting special treatment versus GOPATH/Modules as go doc net work without creation of using go.mod.
I looked around and suspect I missed some doc about this, any pointers appreciated.
(update: using 1.19beta1)

Related

Why golang will remove a necessary package when I go get some other package

When I execute go get [repo_b], go module outputs the following message and I found that go module removed two necessary package, which is required by the project and therefore causing a building failure.
Can anyone tell me what is happening?
go: downloading [repo_a] v1.1.7
go: downloading gorm.io/driver/sqlite v1.3.5
go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.
go: added [repo_b] v0.5.11
go: removed [required_repo_a] v0.0.0-some_commit_hash
go: removed [required_repo_b] v0.0.0-some_commit_hash
I have gone through official go module docs https://go.dev/ref/mod#go-get and search for "remove".
The docs mention that in some case such as ambiguous import, go module will automatically remove the package but I believe they should not be the case...

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

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+

Module declared as X but was required as Y

I'm trying to use grafana/grafana/pkg/tsdb package in my module. I don't think this problem is specific to grafana but here it goes:
$ go get -u github.com/grafana/grafana/pkg/tsdb
go: finding github.com/inconshreveable/log15 latest
go: finding github.com/go-macaron/session latest
go: finding golang.org/x/oauth2 latest
go: finding github.com/teris-io/shortid latest
go: github.com/grafana/grafana/pkg/tsdb imports
github.com/go-xorm/core: github.com/go-xorm/core#v0.6.3: parsing go.mod:
module declares its path as: xorm.io/core
but was required as: github.com/go-xorm/core
It says that the package tsdb is importing xorm as github.com/go-xorm/core, but the module declares itself as xorm.io/core.
Looking at Grafana's go.mod file, it's using github.com/go-xorm/core and going to github.com/go-xorm/core, it says the project is now archived... and it's go.mod file indeed declared itself as xorm.io/core...
and suggestions on how I can resolve this issue?
edit: I also had luck just using a slightly older version:
go get github.com/grafana/grafana/pkg/tsdb#6.6.1
I tried a replace, which can work sometimes:
module foo
replace github.com/go-xorm/core => xorm.io/core v0.6.2
go 1.13
require (
...
but I get a type error.
Luckily it looks like there is a PR out to fix this issue: https://github.com/grafana/grafana/pull/22376

go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision ...?

My coworker pushed a tag v3.0.1 before updating go.mod to have /v3 suffix (https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher). I have updated module path (go.mod) and all import paths (*.go) to fix it, tagged as v3.0.2.
Now the problem is:
go get -v git.example.com/owner/repo#v3.0.2
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
Found this: go build keeps complaining that: go.mod has post-v0 module path
So, I deleted both v3.0.0 and v3.0.1 tags, pointed it to the latest commit, re-pushed but the problem still stand.
I noticed that go.mod still refered to the old version as an indirect dependency:
require (
git.example.com/owner.repo v0.1.2 // indirect
Even if I changed it to /v3 v3.0.2 it will be restored to v0.1.12 automatically.
Why?
Did I miss something?
Tue Jul 23 05:54:56 +07 2019
rm go.*
go mod init git.example.com/dependent/project
go mod tidy
and go.mod is updated correctly now:
require (
- git.example.com/owner/repo v0.1.2
+ git.example.com/owner/repo/v3 v3.0.2
but go get -v git.example.com/owner/repo#v3.0.2 still returned the error:
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
(d85c4f69ad17 is the latest commit in master)
I noticed that there are both v0.1.2 and v3.0.2 in go.sum:
git.example.com/owner/repo v0.1.2 h1:mCGJEmyrFDTCGkRfUIORpqdrNkSONQ6K+AcTNgxqveY=
git.example.com/owner/repo v0.1.2/go.mod h1:FfUKnyPrARCtAXQZ3BQVJI7h2eJ0UpQBMLg4bNs4Kdc=
git.example.com/owner/repo/v3 v3.0.2 h1:mJtDKLeiP8vMRSZo08i/k/KDbIoZTlKW2aWu7DUBvMM=
git.example.com/owner/repo/v3 v3.0.2/go.mod h1:64LE0ts0Lk9InIQyhPYGmnxs6LZIl6H4Iorl1EXfqxo=
Please pay attention to my go get command:
go get -v git.example.com/owner/repo#v3.0.2
It should be:
go get -v git.example.com/owner/repo/v3#v3.0.2
for example you can replace repository with this hack: https://github.com/golang/go/wiki/Modules
require {
...
}
replace git.example.com/owner.repo v0.1.2 => git.example.com/owner.repo v3.0.2
or you can use go get at the commit hash you want:
go get git.example.com/owner.repo#af044c0995fe
go get will correctly update the dependency files (go.mod, go.sum).
For more information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies
or for last example you should clean cache
remove go.mod and go.sum
go cache clean
go mod vendor
Expanding on the answer from #quanta...
You are doing:
go get -v git.example.com/owner/repo#v3.0.2
Because it is a v3 module, the go get command should include a /v3 before the #:
go get -v git.example.com/owner/repo/v3#v3.0.2
Once a v3.x.y package is a module with its own go.mod, then whenenver you are operating with modules enabled, you pretty much always include the /v3 whenever you reference the v3.x.y module, including in:
arguments to go get on the command line
import statements in .go code for the consumer
require statements in a consumer's go.mod
replace or exclude statements in a consumer's go.mod
the module line of the v3 module's go.mod file
internal import statements in .go code inside the v3 module importing other packages in the v3 module
etc.
One way to think about it is that the module's name is now effectively git.example.com/owner/repo/v3, where its name includes the trailing /v3.
If you are a consumer of a vN module and need to update your import paths in your .go files to include the vN, then
github.com/marwan-at-work/mod is a commonly used tool from the community that automates adding the /vN in all the required spots. Separately, it also automates placing the /vN in all the required spots if you are a module author for a v2+ module.
From the "Semantic Import Versioning" section of the Go modules wiki:
If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.0) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg").
I may have had a similiar issue where I updated a module to use the /v2 import path but go getting the module always returned an error about invalid go.mod
The solution was to go get -u github.com/<me>/<pkg>/v2

Resources