About golang array

huangapple go评论69阅读模式
英文:

About golang array

问题

以下是翻译的结果:

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

结果是 [5 4 3 2 1 0]。
它是如何排序的?

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

结果是:

prog.go:8: 数组字面值中的索引重复:2
prog.go:8: 数组字面值中的索引重复:3
prog.go:8: 数组字面值中的索引重复:4
[进程以非零状态退出]

谁能解释这两个结果?

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

the result is [5 4 3 2 1 0].
How it's sort?

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

the result is

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]

Who can explain the both result?

答案1

得分: 5

我看到Dave Cheney在推特上发了这个消息。
我的理解是:

第一个 - Dave的正确的方法

<number_here>是数组中的索引。这个“设置”了当前的索引..这就是为什么在声明的后面,索引必须在数组中“重置”。所以,第一个数字是5(索引0),第二个“条目”的索引是4:..所以值1将在索引4处:

5 _ _ _ 1 _
         ^ 索引当前在这里

..下一个没有索引,但它将在给定的最后一个索引之后继续。也就是4+1..所以索引5得到值0

5 _ _ _ 1 0
           ^ 索引在这里..它需要被重置

现在索引将超出范围..所以它被设置得更靠后。下一个是2:..所以将下面的值与值3放在一起:

5 _ 3 _ 1 0
     ^ 索引在这里

下一个再次继续,因为它没有索引:

5 _ 3 2 1 0
       ^ 索引在这里

然后最后一个索引是1:..值为4:

5 4 3 2 1 0

第二个 - 你的错误方法。

第二个也是同样的事情 - 但你没有保护当前放置索引的覆盖。让我们逐步进行:

索引0处的值为5

5 _ _ _ _ _ _ _ _
 ^ 索引在这里

索引4处的值为1

5 _ _ _ 1 _ _ _ _
         ^ 索引在这里

索引5处的值为0(记住,它继续):

5 _ _ _ 1 0 _ _ _
           ^ 索引在这里

索引2处的值为3

5 _ 3 _ 1 0 _ _ _
     ^ 索引在这里

索引3处的值为2(再次,它继续):

5 _ 3 2 1 0 _ _ _
       ^ 索引在这里

索引1处的值为4

5 4 3 2 1 0 _ _ _
   ^ 索引在这里...你离覆盖下一个值很近了

索引2处的值为12

5 4 12 2 1 0 _ _ _
    ^^^^^ BOOOM

Boom..

..你覆盖了值3,并将继续这样做,因为索引在剩余值的位置。这就是问题所在。

英文:

I saw Dave Cheney tweet this the other day.
My understanding is this:

First one - Dave's working one

The &lt;number_here&gt;: is an index in the array. This "sets" the current index.. which is why further into the declaration the index must be "reset" back in the array. So, the first number is 5 (index 0), the second "entry" has an index of 4: .. so the value 1 will be at index 4:

5 _ _ _ 1 _
         ^ index is currently here

..the next one has no index but it will continue after the last index given. which is 4+1 .. so index 5 gets the value 0:

5 _ _ _ 1 0
           ^ index is here.. it needs to be reset

Now the index will overrun .. so its gets set further back. The next one is 2: .. so that puts the value below with the value 3:

5 _ 3 _ 1 0
     ^ index is here

Next one again, continues on, since it has no index:

5 _ 3 2 1 0
       ^ index is here

Then the last one has index 1: .. with the value 4:

5 4 3 2 1 0

Second one - your broken one.

The second one is the same thing - but you haven't protected the over-writing of a currently placed index. Lets step through it:

Value 5 at index 0:

5 _ _ _ _ _ _ _ _
 ^ index is here

Value 1 at index 4:

5 _ _ _ 1 _ _ _ _
         ^ index is here

Value 0 at index 5 (remember, it continues on):

5 _ _ _ 1 0 _ _ _
           ^ index is here

Value 3 at index 2:

5 _ 3 _ 1 0 _ _ _
     ^ index is here

Value 2 at index 3 (again, it continues on:

5 _ 3 2 1 0 _ _ _
       ^ index is here

Value 4 at index 1:

5 4 3 2 1 0 _ _ _
   ^ index is here ... you&#39;re awfully close to overwriting the next value

Value 12 at index 2:

5 4 12 2 1 0 _ _ _
    ^^^^^ BOOOM

Boom..

..you've overwritten the value 3 and will continue to do so given where the index is for the remaining values. This is the problem..

答案2

得分: -1

【复合字面量】
复合字面量用于构造结构体、数组、切片和映射的值,并在每次评估时创建一个新值。它们由值的类型后跟用大括号括起来的复合元素列表组成。元素可以是单个表达式或键值对。

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

例如,使用基于iota的键,以下代码是等效的:

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)
}

输出结果:

[5 4 3 2 1 0]
[5 4 3 2 1 0]
[5 4 3 2 1 0]

如果出现冲突,会导致错误,例如,对于隐式的a和显式的b

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)
}

输出结果:

prog.go:6: duplicate index in array literal: 2
prog.go:6: duplicate index in array literal: 3
prog.go:6: duplicate index in array literal: 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

【1】:复合字面量
【2】:Iota

英文:

> Composite literals
>
> Composite literals construct values for structs, arrays, slices, and
> maps and create a new value each time they are evaluated. They consist
> of the type of the value followed by a brace-bound list of composite
> elements. An element may be a single expression or a key-value pair.
>
> Iota
>
> Within a constant declaration, the predeclared identifier iota
> represents successive untyped integer constants. It is reset to 0
> whenever the reserved word const appears in the source and increments
> after each ConstSpec. It can be used to construct a set of related
> constants

For example, using iota based keys, the following are equivalent,

package main

import &quot;fmt&quot;

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)
}

Output:

[5 4 3 2 1 0]
[5 4 3 2 1 0]
[5 4 3 2 1 0]

Collisions cause errors, for example, with a implicit and b explicit,

package main

import &quot;fmt&quot;

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)
}

Output:

prog.go:6: duplicate index in array literal: 2
prog.go:6: duplicate index in array literal: 3
prog.go:6: duplicate index in array literal: 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

huangapple
  • 本文由 发表于 2014年9月8日 09:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/25716346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定