type assertion 的单个返回值形式针对不正确的类型将产生 panic。因此,请始终使用“comma ok”的惯用法。
Bad | Good |
---|---|
t := i.(string) |
t, ok := i.(string) if !ok { // 优雅地处理错误 } |
type assertion 的单个返回值形式针对不正确的类型将产生 panic。因此,请始终使用“comma ok”的惯用法。
Bad | Good |
---|---|
t := i.(string) |
t, ok := i.(string) if !ok { // 优雅地处理错误 } |