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?
I saw Dave Cheney tweet this the other day. My understanding is this:
The <number_here>:
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
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're awfully close to overwriting the next value
Value 12
at index 2:
5 4 12 2 1 0 _ _ _
^^^^^ BOOOM
..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..
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.
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 "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)
}
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 "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)
}
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