处理类型断言失败

类型断言的单返回值形式在遇到类型错误时会直接 panic 。因此,请始终使用 "comma ok" 惯用方法。

BadGood
  1. t := i.(string)
  1. t, ok := i.(string)
  2. if !ok {
  3. // handle the error gracefully
  4. }