Golang的多返回值重载是否只适用于map类型?

huangapple go评论70阅读模式
英文:

Is the Golang multiple return overloading unique to the map type?

问题

这两个都可以工作:

m := make(map[int]int)
elem, ok := m[1]
elem = m[1]

然而,这个是不允许的:

func overload() (int, int) {
    return 1, 1
}

func overload() int {
    return 1
}

func main() {
    x := overload()
    x, y := overload()
}

另外,是否有一个内置语法的列表不是通用的?我一直对什么是特殊语法感到困惑,例如map[string]intmake([]int, 10),以及什么是语言的一部分。

英文:

These both work:

m := make(map[int]int)
elem, ok := m[1]
elem = m[1]

Yet, this not allowed:

func overload() (int, int) {
    return 1, 1
}

func overload() int {
    return 1
}

func main() {
    x := overload()
    x, y := overload()
}

Also, is there a list of built-in syntax that doesn't generalize? I keep getting confused on what is a special syntax, i.e. map[string]int, make([]int, 10) and what is part of the language.

答案1

得分: 7

这是特殊的语法。除了映射键检查之外,至少type assertionchannel receive都有一元和二元版本。在所有这些情况下,第二个元素是一个名为okbool,在文档示例中,对于类型断言,它表示断言是否成功,对于通道接收,它表示通信是否成功(如果通道关闭且为空,则为false)。

for...range有自己不同的一元和二元版本,尽管也许range更明显地是特殊的。

有一个内置函数列表。如果你真的想了解所有的边界情况,可以阅读规范,它非常简短,没有像某些标准文档那样被细节所困扰,一旦你熟悉了这门语言,阅读规范是值得的。(Effective Go常见问题也属于这个类别。)

英文:

It's special syntax. Besides the map key check, at least type assertion and channel receive have one- and two-element versions. In all those cases, the second element is a bool called ok in the doc examples; for type assertions it says whether the assertion succeeded and for channel receives it says whether the communication succeeded (false if the channel is closed and empty).

for...range has its own, different one- and two-element versions, though maybe range is more-obviously special.

There is a list of built-in functions. If you really want to know all of the corner cases, go over the spec--it is pretty short, not bogged down in the sorts of details some standards documents are, and worth the time once you've played with the language a bit. (Effective Go and the FAQ are also in this category.)

答案2

得分: 1

Go语言不支持函数重载,即使两个函数在参数或返回值的数量上有所不同,或者参数具有不同的参数或返回类型。

http://golang.org/doc/faq#overloading

据我所知,没有特殊标识符或特殊规则的列表,可以让内置函数逃脱。然而,它们似乎很少见。

英文:

Go doesn't support overloading even if the two functions have different arity in parameters or return values or even if the parameters have different parameter or return types.

http://golang.org/doc/faq#overloading

There is no list of special identifiers or special rules that the built-ins get away with as far as I know. However, they seem to be few and far in-between.

答案3

得分: 0

你给出的示例并不是方法重载。

第一个示例:判断映射中是否存在键。

m := make(map[int]int)
elem, ok := m[1]
elem = m[1]

elem 将接收到来自映射的值 "1" 或者 "零值",ok 将接收到一个布尔值,如果 "foo" 实际上存在于映射中,则设置为 true。

官方网站:Go 不支持方法和运算符的重载吗?

如果方法调度不需要进行类型匹配,那么方法调度就会变得简单。其他语言的经验告诉我们,具有相同名称但不同签名的各种方法有时很有用,但在实践中可能会令人困惑和脆弱。Go 语言的类型系统在名称匹配和类型一致性方面的要求是一个重要的简化决策。

Go 语言支持可变参数函数和方法。这是在 Go 语言中实现函数和方法重载的另一种方式。

可变参数函数或方法是接受可变数量参数的函数或方法。

示例

o1 := Overload(1, 2, 3)
o2 := Overload(153, 196883, 1729, 1634, 5, 36)
o3 := Overload(1, -2)

如果需要更多详细信息,你可以查看这篇文章:在 Go 语言中实现函数和方法重载

英文:

The example you are given are not method overloading.

1st Example: Show whether key exist in map or not.

m := make(map[int]int)
elem, ok := m[1]
elem = m[1]

elem will receive either the value of "1" from the map or a "zero value" and ok will receive a bool that will be set to true if "foo" was actually present in the map.

Officially Site: Go not support overloading of methods and operators?

> Method dispatch is simplified if it doesn't need to do type matching
> as well. Experience with other languages told us that having a variety
> of methods with the same name but different signatures was
> occasionally useful but that it could also be confusing and fragile in
> practice. Matching only by name and requiring consistency in the types
> was a major simplifying decision in Go's type system.

Golang supports variadic functions and methods. That is a another way you can (more or less) do function and method overloading in Golang.

> A variadic function or method is a function or method that accepts a
> variable number of parameters.

Example:

o1 := Overload(1, 2, 3)
o2 := Overload(153, 196883, 1729, 1634, 5, 36)
o3 := Overload(1, -2)

For more details you can look to this post: Function and Method Overloading in Golang.

huangapple
  • 本文由 发表于 2015年3月22日 11:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29190683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定