英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论