golang数组初始化中的键入项 [英] Keyed items in golang array initialization

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

问题描述

在Dave Cheney的酒吧问答中,我遇到了以下结构:

  a:= [...] int {5,4:1,0,2:3,2,1:4} 
fmt.Println(a)

>> [5 4 3 2 1 0]

Playground Link

看起来你可以在数组的初始化字段中使用键( 4:1,0 表示索引4处的set元素为1,索引5处的元素为0)。我从来没有见过这样的事情。它的用例是什么?为什么不直接设置特定的索引?

解决方案

复合文字可以选择性地提供键(数组和片面文字的索引)。


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


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

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

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

元素获取零值未指定值的元素类型。



您可以使用它来:


  • 更紧凑地初始化数组如果数组/切片具有许多零值并且只有一些非零值,那么切片和切片在枚举元素时跳过(跳过)连续的部分,并且跳过的元素将用零值进行初始化。

  • 指定第一对元素,并指定您想要的长度(max index + 1) array / slice有:

      a:= [] int {10,20,30,99:0} //指定前3个元素和设置长度为100 




spec还包含一个例子:创建一个数组,告诉一个字符是否为元音。这是一个非常紧凑和健谈的方式来初始化数组:

  //元音[ch]如果ch是元音,则为true 
vowels:= [128] bool {'a':true,'e':true,'i':true,'o':true,'u':true,'y':true}

另一个例子:让我们创建一个切片,告诉您一天是周末;星期一为0,星期二为1,星期日为6:

  weekend:= [] bool {5:true ,6:true} //其余的将是假的

甚至更好,你甚至可以省略第二个索引( 6 ),因为它隐含地 6 (previous +1):

  weekend:= [] bool {5:true,true} //其余的将是false 


In a pub quiz by Dave Cheney I came across the following construct:

a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4}
fmt.Println(a)

>> [5 4 3 2 1 0]

(Playground Link)

It seems you can use keys in the initialization fields of an array (4: 1, 0 means set element at index 4 to 1, element at index 5 to 0). I have never seen something like this before. What is its use case? Why not set the particular index directly?

解决方案

In composite literals the key (index in case of array and slice literals) can be optionally provided.

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.

Elements get the zero value of the element type whose value is not specified.

You can use this to:

  • more compactly initialize arrays and slices if the array/slice has many zero values and just a few non-zero values

  • skip ("jump over") contiguous parts when enumerating elements, and the skipped elements will be initialized with the zero values

  • specify the first couple of elements, and still specify the length (max index + 1) you want the array/slice to have:

    a := []int{10, 20, 30, 99:0} // Specify first 3 elements and set length to 100
    

The spec also contains an example: create an array which tells if a character is a vowel. This is a very compact and talkative way to initialize the array:

// vowels[ch] is true if ch is a vowel
vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}

Another example: let's create a slice which tells if a day is weekend; Monday being 0, Tuesday being 1, ... and Sunday being 6:

weekend := []bool{5: true, 6: true} // The rest will be false

Or even better, you can even omit the 2nd index (6) as it will be implicitly 6 (previous +1):

weekend := []bool{5: true, true} // The rest will be false

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

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