This question already has an answer here:
I'm trying to create a function that prints out the len
of a list passed into it, regardless of the type of the list. My naive way of doing this was:
func printLength(lis []interface{}) {
fmt.Printf("Length: %d", len(lis))
}
However, when trying to use it via
func main() {
strs := []string{"Hello,", "World!"}
printLength(strs)
}
It complains saying
cannot use strs (type []string) as type []interface {} in argument to printLength
But, a string
can be used as a interface{}
, so why can't a []string
be used as a []interface{}
?
</div>