看看 Go 的内置函数,我才意识到它们不使用接口(interface),而是使用一个神奇的“类型”。
https://golang.org/src/builtin/builtin.go
那么,如果不使用泛型,这到底是怎么可能的呢?如果没有接口(interface),我将如何编写一个具有类似于 append 的签名(采用任何类型的数组)的函数?
最佳答案
你不可能创建这样的函数。具有这种通用的、神奇的“基因”的函数是 language specification 涵盖的内置函数。 , 列于 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.
查看相关问题:
关于go - Go 内置函数使用泛型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44567728/