Search code examples
goswapassign

how to swap values by index in Golang


In python's numpy, one can swap values by index within a list easily like this:

a[[2, 3, 5]] = a[[5, 2, 3]]

Is there a good way to implement this function in Golang.


Solution

  • Go doesn't have fancy indexing like that; the closest you can do is

    a[2],a[3],a[5] = a[5],a[2],a[3]
    

    using regular indexing and regular tuple assignment.