go - 类型转换接口(interface) slice

标签 go go-reflect go-interface

我很好奇为什么 Go 不将 []T 隐式转换为 []interface{} 而它会隐式转换 T接口(interface){}。我缺少关于此转换的重要信息吗?

例子:

func foo([]interface{}) { /* do something */ }

func main() {
    var a []string = []string{"hello", "world"}
    foo(a)
}

go build 提示

cannot use a (type []string) as type []interface {} in function argument

如果我尝试明确地这样做,同样的事情:b := []interface{}(a) 提示

cannot convert a (type []string) to type []interface {}

所以每次我需要进行这种转换时(这似乎经常出现),我一直在做这样的事情:

b = make([]interface{}, len(a), len(a))
for i := range a {
    b[i] = a[i]
}

是否有更好的方法或标准库函数来帮助进行这些转换?每次我想调用一个可以获取列表的函数时,多写 4 行代码似乎有点愚蠢。整数或字符串。

最佳答案

在 Go 中,有一个通用规则,即语法不应隐藏复杂/代价高昂的操作

string 转换为 interface{} 的时间复杂度为 O(1)。将 []string 转换为 interface{} 也是在 O(1) 时间内完成的,因为 slice 仍然是一个值。但是,将 []string 转换为 []interface{} 的时间复杂度为 O(n),因为 slice 的每个元素都必须转换为 interface{

此规则的一个异常(exception)是转换字符串。在将 string[]byte[]rune 相互转换时,即使转换是“语法”。

没有标准库函数可以为您进行这种转换。不过,您最好的选择就是使用您在问题中提供的代码行:

b := make([]interface{}, len(a))
for i := range a {
    b[i] = a[i]
}

否则,您可以使用 reflect 制作一个,但它会比三行选项慢。反射示例:

func InterfaceSlice(slice interface{}) []interface{} {
    s := reflect.ValueOf(slice)
    if s.Kind() != reflect.Slice {
        panic("InterfaceSlice() given a non-slice type")
    }

    // Keep the distinction between nil and empty slice input
    if s.IsNil() {
        return nil
    }

    ret := make([]interface{}, s.Len())

    for i:=0; i<s.Len(); i++ {
        ret[i] = s.Index(i).Interface()
    }

    return ret
}

关于go - 类型转换接口(interface) slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440112/

相关文章:

转到接口(interface) : interface not implemented even though it is

go - 接口(interface)实现元素的范围

reactjs - 服务器重定向不适用于 ReactJS 代理服务器

xml - 通过 io/ioutil net/http 访问后在 Go 中解析 XML

go - 在go中,为什么打印出来的reflected value和它的interface一样?

go - 如何将断言 reflect.Value 结构返回到我知道它实现的接口(interface)?

go - 存储所有符合相同接口(interface)的类型的构造函数集合

arrays - 修改列表元素中的固定大小数组

memory - 戈朗 : trouble with memory

Golang 将列表对象转换为字符串