Why do I get "undeclared name: any (requires version go1.18 or later)" when using any instead of interface{}? I am using Go 1.18

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

Why do I get "undeclared name: any (requires version go1.18 or later)" when using any instead of interface{}? I am using Go 1.18

问题

当尝试在Go 1.18工具链中使用any代替interface{}时,可能会出现以下错误:

undeclared name: any (requires version go1.18 or later)

为什么会出现这个错误,以及如何解决它?any是在Go 1.18中引入的。

英文:

When trying to use any instead of interface{} with the Go 1.18 toolchain, you can get the error:

undeclared name: any (requires version go1.18 or later)

Why is that, and how can you solve it? any was introduced in Go 1.18.

答案1

得分: 17

如果你的go.mod文件中列出的go版本低于1.18,比如:

module example.com/foo

go 1.17

那么你将会遇到这个错误。将你的go.mod文件中的go版本改为go 1.18应该可以解决这个错误。

一般来说,每个模块的go.mod文件控制着编译该模块时所使用的Go语言版本,这样可以让每个模块的作者以自己的节奏逐步采用新的语言变化。这在Go语言变化设计文档中有更详细的描述。

在playground上的示例,包括一个可以编辑的go.mod文件:https://go.dev/play/p/au6TtTvNsRy

英文:

You will get this error if your go.mod file lists a go version below 1.18, such as:

module example.com/foo

go 1.17

Changing your go.mod to instead read go 1.18 should resolve the error.

In general, each module's go.mod file controls the version of the Go language that is used when compiling that module, which allows for a more gradual adoption of new language changes with each module author opting in at their own pace. This is described in more detail in the Go language changes design document.

Example of this error on the playground, including a go.mod file you can edit: https://go.dev/play/p/au6TtTvNsRy

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

发表评论

匿名网友

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

确定