关于golang阵列

关于golang阵列,go,Go,结果是[54 3 2 1 0]。 怎么样 a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 } fmt.Println(a) 结果是 a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 ,12,11,10} fmt.Println(a) 谁能解释这两个结果呢?前几天我看到戴夫·切尼在推特上这么说。 我的理解是: 第一个-戴夫正在工作 :是数组中的索引。这将“设置”当前索引。。这就是为什么在声明之后,索引必须“重置”回数组中的原因。因此

结果是[54 3 2 1 0]。 怎么样

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

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

谁能解释这两个结果呢?

前几天我看到戴夫·切尼在推特上这么说。 我的理解是:

第一个-戴夫正在工作
是数组中的索引。这将“设置”当前索引。。这就是为什么在声明之后,索引必须“重置”回数组中的原因。因此,第一个数字是
5
(索引0),第二个“条目”的索引是
4:
。。因此值
1
将位于索引4:

prog.go:8: duplicate index in array literal: 2
prog.go:8: duplicate index in array literal: 3
prog.go:8: duplicate index in array literal: 4
 [process exited with non-zero status]
5 _ _ _ _ _ _ _ _
 ^ index is here
..下一个没有索引,但它将在给出最后一个索引后继续。这是
4+1
。。因此索引
5
获取值
0

5 _ _ _ 1 _
         ^ index is currently here

现在索引将溢出。。因此,它被设置得更远。下一个是
2:
。。因此,下面的值与值
3

5 _ _ _ 1 0
           ^ index is here.. it needs to be reset
下一个继续,因为它没有索引:

5 _ 3 _ 1 0
     ^ index is here
然后最后一个具有索引
1:
。。值为4时:

5 _ 3 2 1 0
       ^ index is here
第二个-你的坏的一个。 第二个是相同的,但是您没有保护当前放置索引的过度写入。让我们逐步了解它:

索引0处的值
5

5 4 3 2 1 0
索引4处的值
1

prog.go:8: duplicate index in array literal: 2
prog.go:8: duplicate index in array literal: 3
prog.go:8: duplicate index in array literal: 4
 [process exited with non-zero status]
5 _ _ _ _ _ _ _ _
 ^ index is here
索引5处的值
0
(请记住,它将继续):

索引2处的值
3

5 _ _ _ 1 0 _ _ _
           ^ index is here
5 4 3 2 1 0 _ _ _
   ^ index is here ... you're awfully close to overwriting the next value
索引3处的值
2
(同样,它继续:

5 _ _ _ 1 _ _ _ _
         ^ index is here
5 _ 3 _ 1 0 _ _ _
     ^ index is here
索引1处的值
4

5 _ 3 2 1 0 _ _ _
       ^ index is here
索引2处的值
12

5 _ _ _ 1 0 _ _ _
           ^ index is here
5 4 3 2 1 0 _ _ _
   ^ index is here ... you're awfully close to overwriting the next value
繁荣 ..您已经覆盖了值3,并且如果索引用于剩余值,则将继续这样做。这就是问题所在

复合文字为结构、数组、切片和 映射并在每次评估时创建新值。它们包括 值的类型,后跟一个大括号范围的复合值列表 元素。元素可以是单个表达式或键值对

在常量声明中,预先声明的标识符为iota 表示连续的非类型化整数常量。它将重置为0 每当保留字const出现在源中并递增时 在每个ConstSpec之后。它可以用来构造一组相关的 常数

例如,使用基于物联网的密钥,以下是等效的:

5 4 12 2 1 0 _ _ _
    ^^^^^ BOOOM
输出:

package main

import "fmt"

func main() {
    a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4}
    fmt.Println(a)
    b := [...]int{0: 5, 4: 1, 5: 0, 2: 3, 3: 2, 1: 4}
    fmt.Println(b)
    c := make([]int, 6)
    i := 0
    c[i] = 5
    i = 4
    c[i] = 1
    i++
    c[i] = 0
    i = 2
    c[i] = 3
    i++
    c[i] = 2
    i = 1
    c[i] = 4
    fmt.Println(c)
}
package main

import "fmt"

func main() {
    a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4, 12, 11, 10}
    fmt.Println(a)
    b := [...]int{0: 5, 4: 1, 5: 0, 2: 3, 3: 2, 1: 4, 2: 12, 3: 11, 4: 10}
    fmt.Println(b)
}
冲突会导致错误,例如,
a
隐式和
b
显式

[5 4 3 2 1 0]
[5 4 3 2 1 0]
[5 4 3 2 1 0]
输出:

package main

import "fmt"

func main() {
    a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4}
    fmt.Println(a)
    b := [...]int{0: 5, 4: 1, 5: 0, 2: 3, 3: 2, 1: 4}
    fmt.Println(b)
    c := make([]int, 6)
    i := 0
    c[i] = 5
    i = 4
    c[i] = 1
    i++
    c[i] = 0
    i = 2
    c[i] = 3
    i++
    c[i] = 2
    i = 1
    c[i] = 4
    fmt.Println(c)
}
package main

import "fmt"

func main() {
    a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4, 12, 11, 10}
    fmt.Println(a)
    b := [...]int{0: 5, 4: 1, 5: 0, 2: 3, 3: 2, 1: 4, 2: 12, 3: 11, 4: 10}
    fmt.Println(b)
}

现在索引将溢出..因此它将被设置得更远。下一个是2:…因此将值与值3放在下面:为什么下一个是2:当它被设置得更远时?2低于上一个索引-即5。如果允许它继续运行,它将超过数组的末尾,并且将分配更多的空间ated…因此,示例/琐事在输出中会有一个缺口。谢谢。我知道了。值12分配给索引2:值4分配给索引1:。它有一个冲突。哇,我不知道数组文本中的索引。Wild。我理解你为什么提到iota。非常感谢。