go 1.14 to 1.17 update -> modules not working anymore

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

go 1.14 to 1.17 update -> modules not working anymore

问题

在将Go从1.14更新到1.17之后,我遇到了这个错误:

main.go:10:2: 没有所需的模块提供了 github.com/gin-gonic/gin \
在当前目录或任何父目录中找不到 go.mod 文件\
请参阅 'go help modules'

我过去可以通过 go get github.com/gin-gonic/gin 来解决这个问题,但现在这个方法不起作用了。有什么改变吗?

英文:

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?

答案1

得分: 3

我可以重现这个问题,如果我有一个像这样的文件:

package hello
import _ "github.com/gin-gonic/gin"

并运行以下命令:

go mod init hello
go build

修复方法是运行以下命令:

go mod tidy
英文:

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

答案2

得分: 3

Go 1.16开始,“无论当前工作目录或父目录中是否存在go.mod文件,模块感知模式都会默认启用。更准确地说,GO111MODULE环境变量现在默认为on。”

请参阅Migrating to Go Modules博文,了解如何进行迁移的快速概述,或者参阅Tutorial: Create a Go Module获取更详细的信息。

英文:

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.

答案3

得分: 0

根据文档,我们必须在go 1.17+中使用go install example.com/cmd@latest代替go get

英文:

According to documentation we must use go install example.com/cmd@latest instead of go get for go 1.17+

huangapple
  • 本文由 发表于 2021年9月2日 14:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/69025385.html
匿名

发表评论

匿名网友

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

确定