It seems that “[]T” and “[]interface{}” are not compatible.
Should I convert []T to []interface{}?
In this example where you want to take a slice of strings and pass them to a ...interface{}
parameter, then yes, the way you’re doing it is fine.
1 Like
Hi @Yushihu,
The reason fmt.Println(x...)
does not work is that a slice of strings has a different memory layout than a slice of any
(or interface{}
). (You can do a fmt.Println(x)
, however, which compiles and runs fine.)
See this answer in the Go FAQ, or this long but insightful explanation by Eli Bendersky.
Tip: You can do the loop without appending:
args := make([]any, len(x))
for i := range x {
args[i] = x[i]
}
1 Like
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.