doumeng4400
doumeng4400
2013-12-11 11:54
浏览 11
已采纳

需要澄清golang中的动态类型

Considering the fact that is statically typed language, What is the meaning of a dynamic type of some value ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxiangchan0743
    dongxiangchan0743 2013-12-11 12:02
    已采纳

    The 'dynamic type' of a variable is important when handling interface values. Dynamic types are defined as follows (source):

    The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type also have a distinct dynamic type, which is the actual type of the value stored in the variable at run time. The dynamic type may vary during execution but is always assignable to the static type of the interface variable. For non-interface types, the dynamic type is always the static type.

    Consider this example:

    var someValue interface{} = 2
    

    The static type of someValue is interface{} but the dynamic type is int and may very well change in the future. Example:

    var someValue interface{} = 2
    
    someValue = "foo"
    

    In the example above the dynamic type of someValue changed from int to string.

    点赞 评论

相关推荐