I have the following [][]interface{}:
[
[1 65535 62242] [1 65531 20924] [1 96 229] [1 52 208] [1 58 207]
[2 65535 62680] [2 65531 20654] [2 28 212] [2 64 211] [2 17 210] [2 75 208]
[3 65535 62297] [3 65531 20694] [3 30 208] [3 87 207] [3 3 205] [3 36 204]
]
I need to move the elements in order to get the following result:
[
[1 65535 62242] [1 65531 20924] [1 96 229] [1 52 208] [1 58 207]
[3 65535 62297] [3 65531 20694] [3 30 208] [3 87 207] [3 3 205] [3 36 204]
[2 65535 62680] [2 65531 20654] [2 28 212] [2 64 211] [2 17 210] [2 75 208]
]
I have checked the 'Slicetricks' page but after testing and testing I can't find the way to do it.
This is a slice with 17 elements, each one is another slice. I need to move the elements from positions 11:16 (those slices which first element is 3) to position 5:10
If you understand how slices reference an underlying array (if you don't, read https://blog.golang.org/go-slices-usage-and-internals), and you have specific elements you want to swap, you can create two slices over those elements and swap them in order. If all you care about is moving things around, what you're moving doesn't really matter.
sl0 = []AnyType{...}
sl1 = sl0[5:11] // last element is exclusive, so use to 11 if you need 10
sl2 = sl0[11:]
for i := 0; i < len(sl1); i++ {
sl1[i], sl2[i] = sl2[i], sl1[i]
}
After this, sl0 will contain the entire slice with the specific elements swapped.
Just be careful since there is all sorts of potential for bad indexes with this approach. You should be using checks to catch these before they fail in runtime.
Playground example: https://play.golang.org/p/WM7M2buGlil