In Python, I have the following:
i = series.index(s) # standard Python list.index() function
tmp = series.pop(i)
blah = f(tmp)
series.append(tmp)
In converting this to Go, I am looking for a similar way of retrieving an item from a slice by index, doing something with it, then putting the original item at the end of my slice.
From here, I have arrived at the following:
i = Index(series, s) // my custom index function...
tmp, series = series[i], series[i+1:]
blah := f(tmp)
series = append(series, tmp)
But this fails at the end of lists:
panic: runtime error: slice bounds out of range
How would I idiomatically translate this slice.pop()
into Go?
i
and what'scap
andlen
ofseries
? – BarbicanIndex
function. The wiki you listed also has a recipe to pop an element and get the rest of the slice. – Orectici
, corresponding to the index ofseries
where the values
is located. – Kukrilist.index
orlist.pop
with an index argument in Python, it's a sign you may be using the wrong data structure. A set or dict (a map in Go) may be more appropriate, or possibly something else. – Palladicblah
. – Palladic