GO显式数组初始化 [英] GO explicit array initialization

查看:163
本文介绍了GO显式数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GO中是否有显式数组初始化(声明和赋值)还是唯一的方法是使用简写运算符?
这里是一个实际的例子 - 这是两个相等的:

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

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


解决方案

它们是等价的。一般来说:规格:简短变量声明


短变量声明使用以下语法:

  ShortVarDecl = IdentifierList:=ExpressionList。 

这是一个普通的 variable declaration with initializer expressions but no types:

 var IdentifierList =表达式列表。 


所以这一行:

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

相当于此:

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

但是由于表达式列表是 [3] int 的ref / spec#Composite_literalsrel =nofollow noreferrer>复合文字,以下内容相同:

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

规格:变量声明:


如果存在一个类型,则给每个变量赋予该类型。否则,每个变量都被赋予赋值中相应初始化值的类型。


因此,您可以使用以下任何一种声明并初始化 [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}

注意在复合文字中,没有列出所有的值是有效的。 其值未明确指定的元素将是元素类型的零值。您可以在枚举中的值之前包含一个可选索引,以指定其值将为的元素。



规格:复合文字:
$ b


对于数组和切片文字,以下规则适用:


  • 每个元素都有一个相关的整数索引,标记它在数组中的位置。

  • 带键的元素使用键作为其索引;该键必须是一个常数整数表达式。

  • 没有键的元素使用前一个元素的索引加1。如果第一个元素没有键,那么它的索引是零。

由于您的初始数组包含 0 这是元素类型 int 的零值,您可以将其从文字中排除。要创建变量并将其初始化为值 [3] int {369,0,963} ,您也可以这样做:

  //索引1处的值隐式获得0:
g:= [3] int {369,2:963}
h:= [ ...] int {369,2:963}

尝试 Go Playground



有关详情,请参阅此问题详细信息+实际示例: golang数组初始化中的键控项 strong>


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}

解决方案

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

这篇关于GO显式数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆