Golang multiple return 重载是地图类型特有的吗?
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]int
、make([]int, 10)
以及什么是语言的一部分。
Go 不支持重载,即使两个函数在参数或 return 值中具有不同的元数,或者即使参数具有不同的参数或 return 类型。
http://golang.org/doc/faq#overloading
据我所知,没有内置插件可以逃脱的特殊标识符或特殊规则列表。然而,他们似乎很少而且介于两者之间。
给你的例子不是方法重载。
第一个例子:显示key是否存在于map中。
m := make(map[int]int)
elem, ok := m[1]
elem = m[1]
elem
将从地图或 "zero value" 接收值“1”,并且 ok 将接收一个布尔值,如果 "foo" 实际存在则该值将设置为 true在地图上。
官方网站: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 支持可变函数和方法。这是您可以(或多或少)在 Golang 中进行函数和方法重载的另一种方式。
A variadic function or method is a function or method that accepts a
variable number of parameters.
o1 := Overload(1, 2, 3)
o2 := Overload(153, 196883, 1729, 1634, 5, 36)
o3 := Overload(1, -2)
有关详细信息,您可以查看此 post:Function and Method Overloading in Golang.
这是一种特殊的语法。除了映射键检查,至少 type assertion and channel receive 有一个和两个元素版本。在所有这些情况下,第二个元素是文档示例中称为 ok
的 bool
;对于类型断言,它表示断言是否成功,对于通道接收,它表示通信是否成功(false
如果通道关闭且为空)。
for...range
有自己的不同的一元素和二元素版本,尽管 range
可能更明显地特别。
还有一个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也属于这一类。)
这两个都有效:
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]int
、make([]int, 10)
以及什么是语言的一部分。
Go 不支持重载,即使两个函数在参数或 return 值中具有不同的元数,或者即使参数具有不同的参数或 return 类型。
http://golang.org/doc/faq#overloading
据我所知,没有内置插件可以逃脱的特殊标识符或特殊规则列表。然而,他们似乎很少而且介于两者之间。
给你的例子不是方法重载。
第一个例子:显示key是否存在于map中。
m := make(map[int]int)
elem, ok := m[1]
elem = m[1]
elem
将从地图或 "zero value" 接收值“1”,并且 ok 将接收一个布尔值,如果 "foo" 实际存在则该值将设置为 true在地图上。
官方网站: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 支持可变函数和方法。这是您可以(或多或少)在 Golang 中进行函数和方法重载的另一种方式。
A variadic function or method is a function or method that accepts a variable number of parameters.
o1 := Overload(1, 2, 3)
o2 := Overload(153, 196883, 1729, 1634, 5, 36)
o3 := Overload(1, -2)
有关详细信息,您可以查看此 post:Function and Method Overloading in Golang.
这是一种特殊的语法。除了映射键检查,至少 type assertion and channel receive 有一个和两个元素版本。在所有这些情况下,第二个元素是文档示例中称为 ok
的 bool
;对于类型断言,它表示断言是否成功,对于通道接收,它表示通信是否成功(false
如果通道关闭且为空)。
for...range
有自己的不同的一元素和二元素版本,尽管 range
可能更明显地特别。
还有一个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也属于这一类。)