运行go get后,没有提供所需的模块包。

huangapple go评论80阅读模式
英文:

No required module provides package after running go get

问题

所以我将GOPATH设置为我的工作目录,并创建了一个名为file.go的新脚本文件。在该脚本中,我使用了import "github.com/..."。我运行了go get github.com/...,它似乎已经下载了,但是当我运行go run file.go时,仍然出现以下错误:no required module provides package github.com/gordonklaus/portaudio: go.mod file not found in current directory or any parent directory; see 'go help modules'

我尝试添加了go.mod文件,但是错误消息变成了:

go: no module declaration in go.mod. To specify the module path:
        go mod edit -module=example.com/mod

所以我在我的go.mod文件中添加了以下内容,现在它看起来像这样:

module MyModuleName

go 1.17

require(
	"github.com/..." v1.2.3
)

但是我得到了以下错误:

missing go.sum entry for module providing package github.com/...; to add:
        go mod download github.com/...

但是当我运行go mod download github.com/...时,我得到了以下错误:

github.com/...@v1.2.3: invalid version: unknown revision v1.2.3

所以我不知道接下来该怎么办了。

英文:

So I have a GOPATH set to my working directory and I create a new script file.go. In that script I use I import "github.com/..." I ran 'go get github.com/... and it appeared to download but when running go run file.go I still get: no required module provides package github.com/gordonklaus/portaudio: go.mod file not found in current directory or any parent directory; see 'go help modules'.

I tried adding go.mod but then the error just changes to:

go: no module declaration in go.mod. To specify the module path:
        go mod edit -module=example.com/mod

So I adding the following to my go.mod file and it looks like this now:

module MyModuleName

go 1.17

require(
	"github.com/..." v1.2.3
)

But I get:

missing go.sum entry for module providing package github.com/...; to add:
        go mod download github.com/...

But when I run go mod download github.com/... I get:

github.com/...@v1.2.3: invalid version: unknown revision v1.2.3

So I don't know what todo from here.

答案1

得分: 1

我会:

  • 编辑 go.mod 文件并删除包含 v1.2.3 的那一行
  • 在项目文件夹(包含 go.mod 文件)中执行 go get github.com/gordonklaus/portaudi
  • 执行 go mod tidy

最终结果应该是将该库添加,并使用与其最新发布版本匹配的标签。

英文:

I would:

  • edit go.mod and remove the line where v1.2.3 is
  • go get github.com/gordonklaus/portaudi (executed in the project folder, where go.mod is)
  • go mod tidy

The end result should be the library added with a tag matching its latest release.

答案2

得分: 0

如果有人遇到类似的错误:

在尝试运行类似 go run test.go 的 Go 代码时出现了 go.mod 中没有模块声明的错误。以下方法可以帮助我消除这个错误:

GO111MODULE=on go run test.go 

之后,我就能正常运行 Go 代码了,而无需指定 GO111MODULE 变量。

英文:

If someone having similar error:

go: no module declaration in go.mod. To specify the module path:
        go mod edit -module=example.com/mod

while trying to run some go code like go run test.go, the following helped me to eliminate the error:

GO111MODULE=on go run test.go 

After that I was able to run go code normally, without specifying GO111MODULE variable.

huangapple
  • 本文由 发表于 2022年3月18日 08:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71520554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定