Preface
Currently, I have received a request for recommended data, and the data obtained in the database needs to be randomly sorted and returned to the user. After considering it, there are two ways to use it, one is through the database order by rand(), and the other is the code processing that needs to be used in this article.
The specific implementation steps are as follows
1.Introduce the library
The code is as follows:
import ( "fmt" "math/rand" "time" )
2. Assemble data and sort (Scheme 1)
The code is as follows:
type CategoryEntity struct { GrouponId int64 //Team ID MerchandiseId int64 //Product ID CategoryId int64 //Category ID CategoryTitle string //Category Name} func main() { data := make([]CategoryEntity, 10) data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "vegetable"} data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "Seasoning"} data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "fruit"} data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "Personal Care"} data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "Meat"} data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "Snack food"} data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "Care and oil seasoning"} data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "vegetable"} data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "Home Department Store"} data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "fruit"} ("Before random:", data) //If you do not use (seed int64), the random number you get will be the same every time you run it (().Unix()) //Use, randomize the slice and return it (len(data), func(i, j int) { data[i], data[j] = data[j], data[i] }) ("After random:", data) }
3. Assemble data and sort it (Scheme II)
The code is as follows:
type CategoryEntity struct { GrouponId int64 //Team ID MerchandiseId int64 //Product ID CategoryId int64 //Category ID CategoryTitle string //Category Name} func main() { data := make([]CategoryEntity, 10) data[0] = CategoryEntity{GrouponId: 0, MerchandiseId: 1117891, CategoryId: 726, CategoryTitle: "vegetable"} data[1] = CategoryEntity{GrouponId: 1, MerchandiseId: 1110162, CategoryId: 1505, CategoryTitle: "Seasoning"} data[2] = CategoryEntity{GrouponId: 2, MerchandiseId: 1117822, CategoryId: 746, CategoryTitle: "fruit"} data[3] = CategoryEntity{GrouponId: 3, MerchandiseId: 1115770, CategoryId: 1408, CategoryTitle: "Personal Care"} data[4] = CategoryEntity{GrouponId: 4, MerchandiseId: 1116528, CategoryId: 732, CategoryTitle: "Meat"} data[5] = CategoryEntity{GrouponId: 5, MerchandiseId: 1116526, CategoryId: 727, CategoryTitle: "Snack food"} data[6] = CategoryEntity{GrouponId: 6, MerchandiseId: 1117188, CategoryId: 728, CategoryTitle: "Care and oil seasoning"} data[7] = CategoryEntity{GrouponId: 7, MerchandiseId: 1117379, CategoryId: 726, CategoryTitle: "vegetable"} data[8] = CategoryEntity{GrouponId: 8, MerchandiseId: 1118166, CategoryId: 1005, CategoryTitle: "Home Department Store"} data[9] = CategoryEntity{GrouponId: 9, MerchandiseId: 1117377, CategoryId: 746, CategoryTitle: "fruit"} ("Before random:", data) //If you do not use (seed int64), the random number you get will be the same every time you run it (().Unix()) length := len(data) for i := 0; i < length; i++ { exchange(data, (length), i) } ("After random:", data) } // Exchange datafunc exchange(data []CategoryEntity, i, j int) { data[i], data[j] = data[j], data[i] }
Summarize
Overall, it is relatively simple, but there are two things to note:
1: How to use rand in golang. If you don't use (seed int64), the random numbers you get will be the same every time you run it
2: Solution: One uses + anonymous function, randomizes the slice and returns it.
3: Solution 2 uses golang's unique array exchange method:
func exchange(data []CategoryEntity, i, j int) { data[i], data[j] = data[j], data[i] }
This is the end of this article about the implementation of random sorting of golang arrays. For more related random sorting of golang arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!