gpt4 book ai didi

go - 如何从 []interface{} 转换为 []int?

转载 作者:数据小太阳 更新时间:2023-10-29 03:06:35 26 4
gpt4 key购买 nike

<分区>

我想得到非重复的 []int。我正在使用 set,但我不知道如何从 set 中获取 []int。我该怎么做?

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com