You can't simply convert []interface{}
to []string
even if all the values are of concrete type string
, because those 2 types have different memory layout / representation. For details see Cannot convert []string to []interface {}.
You have to define how you want values of different types to be represented by string
values.
The easiest and sensible way would be to iterate over the values, and use fmt.Sprint()
to obtain a string
representation of each, e.g.:
t := []interface{}{
"zero",
1, 2.0, 3.14,
[]int{4, 5},
struct{ X, Y int }{6, 7},
}
fmt.Println(t)
s := make([]string, len(t))
for i, v := range t {
s[i] = fmt.Sprint(v)
}
fmt.Println(s)
fmt.Printf("%q
", s)
Output (try it on the Go Playground):
[zero 1 2 3.14 [4 5] {6 7}]
[zero 1 2 3.14 [4 5] {6 7}]
["zero" "1" "2" "3.14" "[4 5]" "{6 7}"]