以下摘自The Go Programming Language:
Often, functions use these additional results to indicate some kind of error, either by returning an error as in the call to os.Open, or a bool, usually called ok. If a map lookup, type assertion, or channel receive appears in an assignment in which two results are expected, each produces an additional boolean result:
v, ok = m[key] // map lookup
v, ok = x.(T) // type assertion
v, ok = <-ch // channel receive
As with variable declarations, we can assign unwanted values to the blank identifier:
_, err = io.Copy(dst, src) // discard byte count
_, ok = x.(T)
map
查找,类型断言和从channel
接收数据都会返回两个值,其中一个是表示成功或失败的布尔值。同变量定义一样,不想要的值可以赋给_
。