Type converting slices of interfaces

后端 未结 6 1743
孤街浪徒
孤街浪徒 2020-11-22 04:04

I\'m curious why Go does\'t implicitly convert []T to []interface{} when it will implicitly convert T to interface{}. Is

6条回答
  •  眼角桃花
    2020-11-22 04:52

    Here is the official explanation: https://github.com/golang/go/wiki/InterfaceSlice

    var dataSlice []int = foo()
    var interfaceSlice []interface{} = make([]interface{}, len(dataSlice))
    for i, d := range dataSlice {
        interfaceSlice[i] = d
    }
    

提交回复
热议问题