Type conversion of interface slices in Go: how to convert []interface{} to []string?
Hello everyone! I'm new to Go and I've encountered a problem that seems very strange to me. I have a function that takes `[]interface{}` (a slice of interfaces), but I need to pass it to another function that expects `[]string`. I'm trying to do a simple type cast, but the compiler gives an error.
Here's the code example:
```go
package main
import "fmt"
func printStrings(strings []string) {
for _, s := range strings {
fmt.Println(s)
}
}
func main() {
var data []interface{} = []interface{}{"hello", "world", "golang"}
// Error: cannot use data (type []interface{}) as type []string in argument to printStrings
printStrings(data)
}
```
I understand that `[]interface{}` and `[]string` are different types, but each element in `data` is already
No answers yet.
Log in to leave an answer