douwei6478 2018-04-06 07:12 采纳率: 100%
浏览 10
已采纳

未捕获类型断言中的第二个返回值会导致运行时恐慌[关闭]

Below code causes runtime panic on second Println if assertion fails and second value "OK" is not provided for assignment for return value. however, if the second value is provided runtime panic does not occur. How not assigning return value can cause the panic? is there any good resource to learn about panics in Go?

var i interface{} = "hello" 

    f, ok := i.(float64) //  no runtime panic
    fmt.Println(f, ok)

    f = i.(float64) // panic
    fmt.Println(f)

Update 1: To make the question more clear. I wanted to know how panic occurs at runtime when the second return value is not caught 'ok' and panic does not occur when the second return value is caught (f is assigned to zero value and ok is false)

Update 2: From the discussion I understood, assigning two return values handles the runtime panic, in short, it acts as a safeguard. Marking the answer as correct.

  • 写回答

2条回答 默认 最新

  • doukun8670 2018-04-06 07:20
    关注

    This is how type assertions are defined to work, nothing shocking about that.

    Language Spec: Type assertions:

    For an expression x of interface type and a type T, the primary expression

    x.(T)
    

    asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

    [...] If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs.

    [...] A type assertion used in an assignment or initialization of the special form

    v, ok = x.(T)
    v, ok := x.(T)
    var v, ok = x.(T)
    var v, ok T1 = x.(T)
    

    yields an additional untyped boolean value. The value of ok is true if the assertion holds. Otherwise it is false and the value of v is the zero value for type T. No run-time panic occurs in this case.

    In your case i holds a dynamic value of type string, and yet you try to type-assert a value of float64 from it. So the type assertion is false, and per spec a run-time panic occurs. If you'd write i.(string) => this type assertion is true, so no run-time panic would occur in this case.

    If you use the special form (assigning 2 return values), it will never panic, rather if type assertion does not hold, first value will be the zero value of the type you try to assert, second value will be an untyped boolean value false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了