我想获得非重复的 []int。我正在使用set,但我不知道如何[]int从set. 我怎样才能做到这一点?
package main
import (
"fmt"
"math/rand"
"time"
"github.com/deckarep/golang-set"
)
func pickup(max int, num int) []int {
set := mapset.NewSet()
rand.Seed(time.Now().UnixNano())
for set.Cardinality() < num {
n := rand.Intn(max)
set.Add(n)
}
selected := set.ToSlice()
// Do I need to cast from []interface{} to []int around here?
// selected.([]int) is error.
return selected
}
func main() {
results := pickup(100, 10)
fmt.Println(results)
// some processing using []int...
}
函数式编程
相关分类