2 回答

TA貢獻(xiàn)1795條經(jīng)驗 獲得超7個贊
在您的代碼中,keys和questions是數(shù)據(jù)存儲區(qū)鍵和值的同步切片。因此,請使用切片索引的隨機(jī)序列進(jìn)行訪問questions。例如,要隨機(jī)選擇所有鍵和值片,
package main
import (
"fmt"
"math/rand"
"time"
)
type Key struct{}
type Value interface{}
func main() {
keys := make([]*Key, 5)
values := make([]Value, len(keys))
rand.Seed(time.Now().Unix())
for _, r := range rand.Perm(len(keys)) {
k := keys[r]
v := values[r]
fmt.Println(r, k, v)
}
}
輸出:
2 <nil> <nil>
3 <nil> <nil>
4 <nil> <nil>
0 <nil> <nil>
1 <nil> <nil>
該代碼已被修改為使用rand.Perm函數(shù)。

TA貢獻(xiàn)1735條經(jīng)驗 獲得超5個贊
也許你可以使用包 math/rand
randomQuestion:=questions[rand.Intn(len(questions)]
- 2 回答
- 0 關(guān)注
- 246 瀏覽
添加回答
舉報