go - 有没有办法在 Go 中重载函数?

标签 go overloading

我读到在 Go 中无法重载函数。我的意思是重载,一种让两个函数名称相同但参数不同的方法。

我在 Go 的内置函数中看到了一些奇怪的东西:

假设 ch1 是一个 channel 变量:

ch1 := make(chan string)

可以从这样的 channel 中读取内容:

result := <-ch1

但是也有可能得到这样的状态:

result, status := <-ch1

那么,有没有办法像内置函数那样重载一个函数呢?

最佳答案

The Go Programming Language Specification

Built-in functions

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/

相关文章:

go - Go中不同数组类型的相同方法

c++ - 重载函数产生不明确的错误

c++ - 用抽象类覆盖模板函数

c++ - 为什么当我覆盖其他重载之一时,未覆盖的重载函数没有被继承?

c++ - 如何选择适合 C++ 的重载函数?

go - 将 Go 源代码和覆盖率导入 SonarQube

arrays - 无法访问在另一个函数中更改的结构字段

multithreading - goroutines(以及运行它们的操作系统线程)在被 IO 绑定(bind)操作阻塞时如何表现?

mongodb - 如何获取原始 M 内的值

c# - 在 C# 中清理用户输入 - 最干净的方法?