将 []string 转换为 []interface{}。

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

Convert []string to []interface{}

问题

我只需要翻译代码部分,以下是翻译好的内容:

我只是想写一些像这样的代码:

func (w Writer) WriteVString(strs []string) (int, error) {
    return writeV(func(index int, str interface{}) (int, error) {
        return w.WriteString(str.(string))
    }, strs) // 它不能工作
}

func (w Writer) WriteV(bs [][]byte) (int, error) {
    return writeV(func(index int, b interface{}) (int, error) {
        return w.Write(b.([]byte))
    }, []interface{}{bs...}) // 它也无法编译
}
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
}

我以为interface{}可以表示任何类型,所以[]interface{}也可以表示任何[]type,现在我知道我错了,[]type是一个整体类型,不能被视为[]interface{}

那么,有人可以帮我让这段代码工作起来吗,或者有其他解决方案吗?

PS:我知道[]bytestring可以相互转换,但这实际上不是我的意图,也许还有其他类型而不是[]bytestring

英文:

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

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.

答案1

得分: 58

是的,这是因为interface{}是它自己的类型(而不是任何其他类型的“别名”)。
正如我在“golang中interface{}的含义是什么?”中提到的(如果v是一个interface{}变量):

> 初学者常常误以为“v是任意类型”,但这是错误的。
v不是任意类型;它是interface{}类型。

FAQ中提到:

> 它们在内存中的表示不同
>
> 需要逐个复制元素到目标切片。以下示例将一个int切片转换为interface{}切片:

t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}

Tom L评论中提出了这个示例

package main

import "fmt"

func main() {

    x := []string{"a", "b", "c", "d"}
    fmt.Printf("%T: %v\n", x, x)

    //converting a []string to a []interface{}
    y := make([]interface{}, len(x))
    for i, v := range x {
        y[i] = v
    }
    fmt.Printf("%T: %v\n", y, y)

    //converting a []interface{} to a []string
    z := make([]string, len(y))
    for i, v := range y {
        z[i] = fmt.Sprint(v)
    }
    fmt.Printf("%T: %v\n", z, z)

}
英文:

> now I know I'm wrong, []type is a whole type, can't be considered as []interface{}.

Yes, and that is because interface{} is its own type (and not an "alias" for any other type).
As I mention in "what is the meaning of interface{} in golang?" (if v is a interface{} variable):

> Beginner gophers are led to believe that “v is of any type”, but that is wrong.
v is not of any type; it is of interface{} type.

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{}:

t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}

Tom L propose this example (in the comments):

package main

import "fmt"

func main() {

    x := []string{"a", "b", "c", "d"}
    fmt.Printf("%T: %v\n", x, x)

    //converting a []string to a []interface{}
    y := make([]interface{}, len(x))
    for i, v := range x {
        y[i] = v
    }
    fmt.Printf("%T: %v\n", y, y)

    //converting a []interface{} to a []string
    z := make([]string, len(y))
    for i, v := range y {
        z[i] = fmt.Sprint(v)
    }
    fmt.Printf("%T: %v\n", z, z)

}

答案2

得分: 11

  1. 创建一个实用函数,如下所示:
func ToGenericArray(arr ...interface{}) []interface{} {
	return arr
}
  1. 并使用它:
func yourfunc(arr []interface{})  {
....
}

yourfunc(ToGenericArray([]string{"a", "b", "c"}))
  1. 重要提示:以下代码将无法正常工作
func yourfunc(arr []interface{})  {
....
}

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

yourfunc(ToGenericArray(arr))
英文:
  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))

答案3

得分: 0

使用泛型,与sql包一起使用

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

使用泛型的函数toAnyList可以将输入的切片转换为any类型的切片。在函数内部,首先创建了一个与输入切片长度相同的any类型切片list。然后,使用range循环遍历输入切片,并将每个元素赋值给list中对应的位置。最后,返回转换后的list切片。

英文:

With generics, useful with sql package

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

huangapple
  • 本文由 发表于 2014年12月29日 20:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/27689058.html
匿名

发表评论

匿名网友

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

确定