About golang set
a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 } fmt.Println(a)
the result is [5 4 3 2 1 0]. How to sort it?
a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4 ,12,11,10} fmt.Println(a)
result
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 both results?
source to share
I saw Dave Cheney tweeting this the other day. My understanding is this:
First - Dave is working
<number_here>:
is the index in the array. This "sets" the current index. That is why further in the declaration the index should be "reset" in the array. So the first number 5
(index 0), the second "entry" has index 4:
.. so the value 1
will have index 4:
5 _ _ _ 1 _
^ index is currently here
.. the next one has no index, but it will continue after the last pointer. which is 4+1
.. so the index 5
gets the value 0
:
5 _ _ _ 1 0
^ index is here.. it needs to be reset
The index will now overflow .. so it is set again. The next one is 2:
.., so the value is lower 3
:
5 _ 3 _ 1 0
^ index is here
Then it is repeated again, since it has no index:
5 _ 3 2 1 0
^ index is here
Then the latter has an index 1:
.. with a value of 4:
5 4 3 2 1 0
The second is your broken one.
The second is the same thing, but you have not protected overwriting the currently allocated index. Go through it:
Value 5
with index 0:
5 _ _ _ _ _ _ _ _
^ index is here
Value 1
at index 4:
5 _ _ _ 1 _ _ _ _
^ index is here
The value 0
at index 5 (remember, it continues):
5 _ _ _ 1 0 _ _ _
^ index is here
Value 3
at index 2:
5 _ 3 _ 1 0 _ _ _
^ index is here
The value 2
at index 3 (again, it continues:
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
12
Index value 2:
5 4 12 2 1 0 _ _ _
^^^^^ BOOOM
Boom ..
.. you have overwritten the value 3 and continue to do so by specifying where the index is for the rest of the values. This is problem.
source to share
Composite literals build values ββfor structures, arrays, slices, and maps, and create a new value each time they are evaluated. They consist of a value type followed by a parenthesized list of constituent elements. The element can be a single expression or a key-value pair.
In a constant declaration, the predefined identifier iota represents sequential untyped integer constants. reset 0 whenever a const reserved word appears in source and incremented after each ConstSpec. It can be used to build a set of related Constants
For example, using iota based keys, the following equivalents,
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
source to share