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
Asked Answered
H

1

8

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.

Hernandes answered 15/3, 2022 at 18:48 Comment(0)
H
18

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

Hernandes answered 15/3, 2022 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.