How to convert interface{} to []int?

huangapple go评论107阅读模式
英文:

How to convert interface{} to []int?

问题

我正在使用Go编程语言进行编程。

假设有一个类型为interface{}的变量,其中包含一个整数数组。我该如何将interface{}转换回[]int类型?

我尝试了以下方法:

  1. interface_variable.([]int)

但是我得到了以下错误:

  1. panic: interface conversion: interface is []interface {}, not []int
英文:

I am programming in Go programming language.

Say there's a variable of type interface{} that contains an array of integers. How do I convert interface{} back to []int?

I have tried

  1. interface_variable.([]int)

The error I got is:

  1. panic: interface conversion: interface is []interface {}, not []int

答案1

得分: 15

这是一个[]interface{}而不仅仅是一个interface{},你需要遍历它并进行转换:

2022年的解答

https://go.dev/play/p/yeihkfIZ90U

  1. func ConvertSlice[E any](in []any) (out []E) {
  2. out = make([]E, 0, len(in))
  3. for _, v := range in {
  4. out = append(out, v.(E))
  5. }
  6. return
  7. }

go1.18之前的解答

http://play.golang.org/p/R441h4fVMw

  1. func main() {
  2. a := []interface{}{1, 2, 3, 4, 5}
  3. b := make([]int, len(a))
  4. for i := range a {
  5. b[i] = a[i].(int)
  6. }
  7. fmt.Println(a, b)
  8. }
英文:

It's a []interface{} not just one interface{}, you have to loop through it and convert it:

the 2022 answer

https://go.dev/play/p/yeihkfIZ90U

  1. func ConvertSlice[E any](in []any) (out []E) {
  2. out = make([]E, 0, len(in))
  3. for _, v := range in {
  4. out = append(out, v.(E))
  5. }
  6. return
  7. }

the pre-go1.18 answer

http://play.golang.org/p/R441h4fVMw

  1. func main() {
  2. a := []interface{}{1, 2, 3, 4, 5}
  3. b := make([]int, len(a))
  4. for i := range a {
  5. b[i] = a[i].(int)
  6. }
  7. fmt.Println(a, b)
  8. }

答案2

得分: 2

正如其他人所说,你应该迭代切片并逐个转换对象。
最好在范围内使用类型开关以避免发生恐慌:

a := []interface{}{1, 2, 3, 4, 5}
b := make([]int, len(a))
for i, value := range a {
switch typedValue := value.(type) {
case int:
b[i] = typedValue
break
default:
fmt.Println("不是int类型:", value)
}
}
fmt.Println(a, b)

http://play.golang.org/p/Kbs3rbu2Rw

英文:

As others have said, you should iterate the slice and convert the objects one by one.
Is better to use a type switch inside the range in order to avoid panics:

  1. a := []interface{}{1, 2, 3, 4, 5}
  2. b := make([]int, len(a))
  3. for i, value := range a {
  4. switch typedValue := value.(type) {
  5. case int:
  6. b[i] = typedValue
  7. break
  8. default:
  9. fmt.Println("Not an int: ", value)
  10. }
  11. }
  12. fmt.Println(a, b)

http://play.golang.org/p/Kbs3rbu2Rw

答案3

得分: 0

以下是翻译好的内容:

函数的返回值类型是interface{},但实际返回的值是[]interface{},所以请尝试使用以下代码:

  1. func main() {
  2. values := returnValue.([]interface{})
  3. for i := range values {
  4. fmt.Println(values[i])
  5. }
  6. }
英文:

Func return value is interface{} but real return value is []interface{}, so try this instead:

  1. func main() {
  2. values := returnValue.([]interface{})
  3. for i := range values {
  4. fmt.Println(values[i])
  5. }
  6. }

huangapple
  • 本文由 发表于 2014年6月27日 21:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/24453420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定