You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

709 B

title
逗号ok模式

逗号ok模式

通过逗号ok模式(comma ok idiom)我们可以进行类型断言判断映射中是否存在某个key以及通道是否关闭。

类型断言

// 方式1
var (
    v T
    ok bool
)
v, ok = x.(T)

// 方式2
v, ok := x.(T) // x是接口类型的变量T是要断言的类型

// 方式3
var v, ok = x.(T)

// 方式4
v := x.(T) // 当心此种方式断言,若断言失败会发生恐慌

判断key是否存在映射中

// 方式1
v, ok := a[x]

// 方式2
var v, ok = a[x]

判断通道是否关闭

// 方式1
var (
    x T
    ok bool
)
x, ok = <-ch

// 方式2
x, ok := <-ch

// 方式3
var x, ok = <-ch