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
.
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.
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.
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.
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 ofinterface{}
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 ofinterface{}
:
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) }
func ToGenericArray(arr ...interface{}) []interface{} { return arr }
func yourfunc(arr []interface{}) { .... } yourfunc(ToGenericArray([...]string{"a", "b", "c"}))
func yourfunc(arr []interface{}) { .... } arr:=[...]string{"a", "b", "c"} yourfunc(ToGenericArray(arr))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With