Type conversion of interface slices in Go: why doesn't this work?
Hello everyone! I'm new to Go and can't figure out type conversion for interface slices. I have a function that takes `[]interface{}`, but I want to pass it a slice of a specific type, like `[]string`. Why doesn't Go allow this directly?\n\nHere's an example code that doesn't work:\n\n```go\npackage main\n\nimport "fmt"\n\nfunc printAll(items []interface{}) {\n for _, item := range items {\n fmt.Println(item)\n }\n}\n\nfunc main() {\n names := []string{"Alice", "Bob", "Charlie"}\n // Error: cannot use names (type []string) as type []interface{} in argument to printAll\n printAll(names)\n}\n```\n\nI understand that `string` implements the `interface{}` interface, but why can't `[]string` be used as `[]interface{}`? In the document
No answers yet.
Log in to leave an answer
Blogs
Caching in Java: Redis, Memcached, and Built-in Solutions from A to Z
Property-based Testing in JavaScript: Catching Bugs You Weren't Looking For
From ArrayList<String> to String[] in Java: A Simple Guide
Code Coverage in PHP: Metrics, Tools, and Life Hacks