Go 内置函数使用泛型吗?
Do Go built-ins use generics?
看看 Go 的内置函数,我才意识到他们不使用接口,而是使用魔法 'Type'。
https://golang.org/src/builtin/builtin.go
那么,如果不使用泛型,这到底是怎么可能的呢?如果没有接口,我将如何编写一个具有类似于 append 的签名(采用任何类型的数组)的函数?
您无法创建此类函数。具有这种通用的、神奇的“基因”的函数是 language specification, listed in section Predeclared identifiers.
涵盖的内置函数
The signature of append
[...] schematically, it's like this:
func append(slice []T, elements ...T) []T
where T
is a placeholder for any given type. You can't actually write a function in Go where the type T
is determined by the caller. That's why append
is built in: it needs support from the compiler.
查看相关问题:
看看 Go 的内置函数,我才意识到他们不使用接口,而是使用魔法 'Type'。
https://golang.org/src/builtin/builtin.go
那么,如果不使用泛型,这到底是怎么可能的呢?如果没有接口,我将如何编写一个具有类似于 append 的签名(采用任何类型的数组)的函数?
您无法创建此类函数。具有这种通用的、神奇的“基因”的函数是 language specification, listed in section Predeclared identifiers.
涵盖的内置函数The signature of
append
[...] schematically, it's like this:func append(slice []T, elements ...T) []T
where
T
is a placeholder for any given type. You can't actually write a function in Go where the typeT
is determined by the caller. That's whyappend
is built in: it needs support from the compiler.
查看相关问题: