When you print the result of a function with 2 return values, you will get
valA valB
Retrieving from a map can return either 1 value or two values:
v := m["Answer"]
v, ok := m["Answer"]
However, when you print m["Answer"]
, you will always only print v
. What exactly is the "function signature" of a map retrieval? Or is this just a special case? Here is an example showing the distinction:
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Answer"] = 42
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present:", ok)
fmt.Println(banana())
fmt.Println(m["Answer"])
}
func banana() (int, int) {
return 1, 2
}
Special case. The same applies to other operations, among others:
read from chan
:
v, ok := <- somechannel
casting, e.g.:
casted, ok := somevar.(sometype)