I tried to translate the following Python code to Go
import random
list = [i for i in range(1, 25)]
random.shuffle(list)
print(list)
but found my Go version lengthy and awkward because there is no shuffle function and I had to implement interfaces and convert types.
What would be an idiomatic Go version of my code?
Since 1.10 Go includes an official Fisher-Yates shuffle function.
Documentation:
pkg/math/rand/#Shuffle
See also the original CL 51891
First, as commented by shelll:
Example:
dystroy's answer is perfectly reasonable, but it's also possible to shuffle without allocating any additional slices.
See this Wikipedia article for more details on the algorithm.
rand.Perm
actually uses this algorithm internally as well.