简体   繁体   English

将 [] 字符串转换为 [] 接口{}

[英]Convert []string to []interface{}

I just want to write some code like this:我只想写一些这样的代码:

func (w Writer) WriteVString(strs []string) (int, error) {
    return writeV(func(index int, str interface{}) (int, error) {
        return w.WriteString(str.(string))
    }, strs) // it doesn't work
}

func (w Writer) WriteV(bs [][]byte) (int, error) {
    return writeV(func(index int, b interface{}) (int, error) {
        return w.Write(b.([]byte))
    }, []interface{}{bs...}) // it also can't be compiled
}
type writeFunc func(int, interface{}) (int, error)

func writeV(fn writeFunc, slice []interface{}) (n int, err error) {
    var m int
    for index, s := range slice {
        if m, err = fn(index, s); err != nil {
            break
        }
        n += m
    )
    return
}

I thought interface{} can represent any type, so []interface can also represent any []type before, now I know I'm wrong, []type is a whole type, can't be considered as []interface{} .我以为interface{}可以代表任何类型,所以之前[]interface也可以代表任何[]type ,现在我知道我错了, []type是一个完整的类型,不能认为是[]interface{} .

So, can anyone help me how to make this code work, or any other solution?那么,任何人都可以帮助我如何使这段代码工作,或任何其他解决方案?

PS : I know that []byte or string can be converted to one another, but it's not actually my intention, may be there is another type rather than []byte and string . PS :我知道[]bytestring可以相互转换,但这实际上不是我的意图,可能有另一种类型而不是[]bytestring

now I know I'm wrong, []type is a whole type, can't be considered as []interface{} . 现在我知道我错了, []type是一个整体类型,不能算是[]interface{}

Yes, and that is because interface{} is its own type (and not an "alias" for any other type). 是的,这是因为interface{}是它自己的类型(而不是任何其他类型的“别名”)。
As I mention in " what is the meaning of interface{} in golang? " (if v is a interface{} variable): 正如我在golang中提到的“ interface{}的含义是什么? ”(如果v是一个interface{}变量):

Beginner gophers are led to believe that “ v is of any type”, but that is wrong. 初学者通过相信“ v是任何类型”,但这是错误的。
v is not of any type; v不属于任何类型; it is of interface{} type. 它是interface{}类型。

The FAQ mentions 常见问题提到

they do not have the same representation in memory . 他们在记忆中没有相同的表现形式

It is necessary to copy the elements individually to the destination slice . 有必要将元素分别复制到目标切片
This example converts a slice of int to a slice of interface{} : 此示例将int切片转换为interface{}的切片:

t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}
  1. Create a utility function, like this创建一个效用函数,像这样
func ToGenericArray(arr ...interface{}) []interface{} {
    return arr
}
  1. And use it:并使用它:
func yourfunc(arr []interface{})  {
....
}

yourfunc(ToGenericArray([...]string{"a", "b", "c"}))

  1. IMPORTANT NOTICE: the following will not work重要提示:以下将不起作用
func yourfunc(arr []interface{})  {
....
}

arr:=[...]string{"a", "b", "c"}

yourfunc(ToGenericArray(arr))

With generics, useful with sql package使用泛型,对 sql 包有用

func toAnyList[T any](input []T) []any{
    list := make([]any, len(input))
    for i, v := range input {
        list[i] = v
    }
    return list
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM