3
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 Answer 1

5

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

First one - Dave's working one

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

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'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..

Sign up to request clarification or add additional context in comments.

5 Comments

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: Why the next one is 2: when its gets set further back?
2 is lower than the last index - which was 5. If it was allowed to keep going it would go past the end of the array and more space would be allocated.
...and thus, the example/trivia would have a gap in the output.
thank you. I got it. The value 12 assign to index 2: after value 4 assign to index 1:. It has a confict.
Wow, had no idea about indices in array literals. Wild.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.