When calling functions or methods with multiple return values, the caller, if unpacking any return values, must unpack them ALL.
f, err := os.Open("filename")
The err
variable must be either declared or ignored with the blank identifier _
, but it cannot be omitted.
However, there are some operations built into the language, which allow one to omit the optional second return value.
Is there a more or less formal list of situations where an operation with a data structure or a function call returns a second value that is optional and can be ignored by only receiving the first return value? For example:
m := make(map[string]int)
v, ok := m["hello"]
The ok
variable is entirely optional and can be omitted.
v := m["hello"]
Likewise, with channels:
v, ok := <-ch
or
v := <-ch
Are there any other cases of that behavior beyond the two above?