我读到在 Go 中无法重载函数。我的意思是重载,一种让两个函数名称相同但参数不同的方法。
我在 Go 的内置函数中看到了一些奇怪的东西:
假设 ch1
是一个 channel 变量:
ch1 := make(chan string)
可以从这样的 channel 中读取内容:
result := <-ch1
但是也有可能得到这样的状态:
result, status := <-ch1
那么,有没有办法像内置函数那样重载一个函数呢?
最佳答案
The Go Programming Language Specification
Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument.
The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.
Go 编程语言不允许函数重载,Go 内置函数除外。您无法重载您的函数。
关于go - 有没有办法在 Go 中重载函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486370/