jaswdr / faker

:rocket: Ultimate fake data generator for Go with zero dependencies
https://pkg.go.dev/github.com/jaswdr/faker
MIT License
566 stars 59 forks source link

Add shuffle slice utility #123

Open lil5 opened 2 years ago

lil5 commented 2 years ago

Is your feature request related to a problem? Please describe. I'm trying to fake a randomized array of unique enums

Describe the solution you'd like To include shuffleSlice as Faker.ShuffleSlice

func ShuffleSlice[T any](slice []T) []T {
    rand.Shuffle(len(slice), func(i, j int) {
        slice[i], slice[j] = slice[j], slice[i]
    })

    return slice
}

go playground example https://play.golang.com/p/1rQ2O-0aNOx

jaswdr commented 1 year ago

Hello @lil5, unfortunately it is not possible to use Generics because we still support versions <= 1.18. I'll add this issue to our go1.18 migration plan. I'll include this idea when we bump our Go version.