如何进行golang 数组随机排序的实现,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
前言
目前接到一个推荐数据的需求,需要将数据库中获取到的数据进行随机排序后返回给用户。考虑了一下,有两种使用方式,一种是通过数据库 order by rand() ,还有一种就是本文需要使用到的代码处理
具体实现步骤如下
1.引入库
代码如下:
import ( "fmt" "math/rand" "time")
2.组装数据并排序(方案一)
代码如下:
type CategoryEntity struct { GrouponId int64 //团ID MerchandiseId int64 //商品ID CategoryId int64 //分类ID CategoryTitle string //分类名称}func main() { data := make([]CategoryEntity, 10) data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "蔬菜"} data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "调料调味"} data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "水果"} data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "个人护理"} data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "肉"} data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "休闲食品"} data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "粮油调味"} data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "蔬菜"} data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "居家百货"} data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "水果"} fmt.Println("随机前:", data) //如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样 rand.Seed(time.Now().Unix()) //采用rand.Shuffle,将切片随机化处理后返回 rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] }) fmt.Println("随机后:", data)}
3.组装数据并排序(方案二)
代码如下:
type CategoryEntity struct { GrouponId int64 //团ID MerchandiseId int64 //商品ID CategoryId int64 //分类ID CategoryTitle string //分类名称}func main() { data := make([]CategoryEntity, 10) data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "蔬菜"} data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "调料调味"} data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "水果"} data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "个人护理"} data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "肉"} data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "休闲食品"} data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "粮油调味"} data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "蔬菜"} data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "居家百货"} data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "水果"} fmt.Println("随机前:", data) //如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样 rand.Seed(time.Now().Unix()) length := len(data) for i := 0; i < length; i++ { exchange(data, rand.Intn(length), i) } fmt.Println("随机后:", data)}// 交换数据func exchange(data []CategoryEntity, i, j int) { data[i], data[j] = data[j], data[i]}
整体比较简单,但是需要注意的有两点:
golang中 rand的使用方式,如果不使用rand.Seed(seed int64),每次运行,得到的随机数会一样
方案一种采用rand.Shuffle+匿名函数,将切片随机化处理后返回。
方案二使用golang特有的数组交换方式:
func exchange(data []CategoryEntity, i, j int) { data[i], data[j] = data[j], data[i]}
关于如何进行golang 数组随机排序的实现问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。