go - Go 内置函数使用泛型吗?

标签 go gcc built-in

看看 Go 的内置函数,我才意识到它们不使用接口(interface),而是使用一个神奇的“类型”。

https://golang.org/src/builtin/builtin.go

那么,如果不使用泛型,这到底是怎么可能的呢?如果没有接口(interface),我将如何编写一个具有类似于 append 的签名(采用任何类型的数组)的函数?

最佳答案

你不可能创建这样的函数。具有这种通用的、神奇的“基因”的函数是 language specification 涵盖的内置函数。 , 列于 Predeclared identifiers 部分.

引自Effective Go: Append:

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 functions accessed through variables

Return map like 'ok' in Golang on normal functions

关于go - Go 内置函数使用泛型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44567728/

相关文章:

google-app-engine - 如何在 Google App Engine 中为 Go 应用程序设置应用程序范围设置?

gcc - 如何从命令行添加 gdi32.lib

.net - 是否有用于组合框的内置 .net string/double 对象?

python - 动态语言 IDE 中的自动完成,特别是 PyDev 中的 Python

regex - 如何解决关于 'filter'字段必须是BSON类型对象的问题

go - panic : http: multiple registrations for/debug/requests

arrays - Go:空花括号对数组初始化内存分配的影响

通过在寄存器中传递函数地址从 asm 文件调用 C 函数

c++ - 用c++17 mac编译

git - 如何使用 Git 别名启动 Bash 函数