dqpwdai095465 2015-09-06 05:24 采纳率: 100%
浏览 132
已采纳

在()中包装类型有什么作用,什么时候应该使用它? Golang [重复]

This question already has an answer here:

What exactly does wrapping a type in () do exactly and when should I use it? E.g. blah.(*int)

</div>
  • 写回答

1条回答 默认 最新

  • dongyu8694 2015-09-06 05:33
    关注

    It's a type assertion. Type assertions are used to extract the value in an interface type as some other type.

    The expression blah.(*int) asserts that the type of the value in blah is *int. If the assertion holds, then the value of the expression is the value stored in blah as a *int. If the assertion does not hold, then the expression panics.

    A special form of a type assertion can be used in an assignment to test the assertion:

     ip, ok := blah.(*int)
    

    If the value in blah is of *int, then the value is stored in ip and ok is set to true. Otherwise, ip is set to the zero value and ok is false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号