golang 数组随机排序的实现
程序员文章站
2022-06-09 21:05:45
目录前言具体实现步骤如下1.引入库2.组装数据并排序(方案一)3.组装数据并排序(方案二)总结前言目前接到一个推荐数据的需求,需要将数据库中获取到的数据进行随机排序后返回给用户。考虑了一下,有两种使用...
前言
目前接到一个推荐数据的需求,需要将数据库中获取到的数据进行随机排序后返回给用户。考虑了一下,有两种使用方式,一种是通过数据库 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] }
总结
整体比较简单,但是需要注意的有两点:
1:golang中 rand的使用方式,如果不使用rand.seed(seed int64),每次运行,得到的随机数会一样
2:方案一种采用rand.shuffle+匿名函数,将切片随机化处理后返回。
3:方案二使用golang特有的数组交换方式:
func exchange(data []categoryentity, i, j int) { data[i], data[j] = data[j], data[i] }
到此这篇关于golang 数组随机排序的实现的文章就介绍到这了,更多相关golang 数组随机排序内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
下一篇: Go container包的介绍