Go 1.18 中的 "any" 类型是什么?

What is the "any" type in Go 1.18?

在Visual Studio代码中,自动完成工具(我猜是gopls?)给出了以下模板:

m.Range(func(key, value any) bool {
    
})

其中 msync.Map。类型 any 无法识别,但被放在那里。

什么是any?我可以放我想要的类型并希望 Go 1.18 为我做 implicit 类型转换吗?例如:

m.Range(func(k, v string) { ... })

这将在回调中将 kv 作为字符串提供,而无需自己进行类型转换?

any 是一个新的 predeclared identifierinterface{}.

的类型别名

来自issue 49884, CL 368254 and commit 2580d0e.

问题提到interface{}/any:

It's not a special design, but a logical consequence of Go's type declaration syntax.

You can use anonymous interfaces with more than zero methods:

func f(a interface{Foo(); Bar()}) {
   a.Foo()
   a.Bar()
}

Analogous to how you can use anonymous structs anywhere a type is expected:

func f(a struct{Foo int; Bar string}) {
   fmt.Println(a.Foo)
   fmt.Println(a.Bar)
}

An empty interface just happens to match all types because all types have at least zero methods.
Removing interface{} would mean removing all interface functionality from the language if you want to stay consistent / don't want to introduce a special case.