douzhao5656 2016-08-05 07:10
浏览 86
已采纳

GO显式数组初始化

Is there explicit array initialization (declaration and assignment) in GO or the only way is using the shorthand operator? Here is a practical example - is this two equal:

a := [3]int{1, 0, 1}

var a [3]int = [3]int{1, 0, 1}
  • 写回答

1条回答 默认 最新

  • dsxcv5652 2016-08-05 07:15
    关注

    They are equivalent. In general: Spec: Short variable declaration:

    A short variable declaration uses the syntax:

    ShortVarDecl = IdentifierList ":=" ExpressionList .
    

    It is shorthand for a regular variable declaration with initializer expressions but no types:

    "var" IdentifierList = ExpressionList .
    

    So this line:

    a := [3]int{369, 0, 963}
    

    Is equivalent to this:

    var a = [3]int{369, 0, 963}
    

    But since the expression list is a composite literal of type [3]int, the following is the same:

    var a [3]int = [3]int{369, 0, 963}
    

    Spec: Variable declaration:

    If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment.

    So you may use any of the following, all declare and initialize a variable of type [3]int:

    a := [3]int{369, 0, 963}
    b := [...]int{369, 0, 963}
    var c = [3]int{369, 0, 963}
    var d [3]int = [3]int{369, 0, 963}
    var e [3]int = [...]int{369, 0, 963}
    var f = [...]int{369, 0, 963}
    

    Notes:

    Note that in composite literals, it is valid to not list all values. Elements whose value is not explicitly specified will be the zero value of the element type. You may include an optional index before a value in the enumeration to specify the element whose value it will be.

    Spec: Composite literals:

    For array and slice literals the following rules apply:

    • Each element has an associated integer index marking its position in the array.
    • An element with a key uses the key as its index; the key must be a constant integer expression.
    • An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.

    Since your initial array contains a 0 which is the zero value for the element type int, you may exclude it from the literal. To create and initialize a variable to the value [3]int{369, 0, 963}, you may also do it like this:

    // Value at index 1 implicitly gets 0:
    g := [3]int{369, 2: 963} 
    h := [...]int{369, 2: 963} 
    

    Try all the examples on the Go Playground.

    See this question for more details + practical examples: Keyed items in golang array initialization

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能