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]
}

總結

整體比較簡單,但是需要註意的有兩點:

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 數組隨機排序內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: