Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 979
zhuah Avatar asked Dec 29 '14 12:12

zhuah


People also ask

How to convert interface{} to string?

To convert interface to string in Go, use fmt. Sprint function, which gets the default string representation of any value. If you want to format an interface using a non-default format, use fmt. Sprintf with %v verb.

What is [] interface {} Golang?

interface{} means you can put value of any type, including your own custom type. All types in Go satisfy an empty interface ( interface{} is an empty interface). In your example, Msg field can have value of any type.

How do you implement an interface in Golang?

To implement an interface, you just need to implement all the methods declared in the interface. Unlike other languages like Java, you don't need to explicitly specify that a type implements an interface using something like an implements keyword.


2 Answers

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)  } 
like image 142
VonC Avatar answered Oct 08 '22 05:10

VonC


  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))  
like image 23
jashawnpeng Avatar answered Oct 08 '22 05:10

jashawnpeng