词典遍历顺序
func mapKeyOrder() { m := map[int]struct{}{} for i := 0; i < 16; i++ { m[i] = struct{}{} } for i := 0; i < 16; i++ { l := []int{} for k := range m { l = append(l, k) } log.Println(l) } }
2022/08/24 14:27:46 [2 11 1 3 5 6 7 10 12 14 0 8 9 13 15 4]
2022/08/24 14:27:47 [8 9 13 15 4 2 11 1 3 5 6 7 10 12 14 0]
2022/08/24 14:27:47 [8 9 13 15 4 2 11 1 3 5 6 7 10 12 14 0]
2022/08/24 14:27:47 [8 4 9 13 15 1 2 11 12 14 0 3 5 6 7 10]
2022/08/24 14:27:47 [8 4 9 13 15 1 2 11 10 12 14 0 3 5 6 7]
2022/08/24 14:27:47 [4 9 13 15 1 2 11 10 12 14 0 3 5 6 7 8]
2022/08/24 14:27:47 [7 10 12 14 0 3 5 6 8 4 9 13 15 1 2 11]
2022/08/24 14:27:47 [1 2 11 0 3 5 6 7 10 12 14 8 4 9 13 15]
2022/08/24 14:27:47 [5 6 7 10 12 14 0 3 8 13 15 4 9 11 1 2]
2022/08/24 14:27:47 [6 7 10 12 14 0 3 5 8 15 4 9 13 1 2 11]
2022/08/24 14:27:47 [8 4 9 13 15 1 2 11 7 10 12 14 0 3 5 6]
2022/08/24 14:27:47 [8 4 9 13 15 1 2 11 7 10 12 14 0 3 5 6]
2022/08/24 14:27:47 [14 0 3 5 6 7 10 12 8 4 9 13 15 1 2 11]
2022/08/24 14:27:47 [1 2 11 14 0 3 5 6 7 10 12 8 4 9 13 15]
2022/08/24 14:27:47 [1 2 11 0 3 5 6 7 10 12 14 8 4 9 13 15]
2022/08/24 14:27:47 [8 15 4 9 13 1 2 11 6 7 10 12 14 0 3 5]
Shuffle a slice or array · YourBasic Go https://yourbasic.org/golang/shuffle-slice-array/
a := []int{1, 2, 3, 4, 5, 6, 7, 8}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
[5 8 6 4 3 7 2 1]