I am trying to write a Map function so that it can handle all types of arrays.
// Interface to specify generic type of array. type Iterable interface { } func main() { list_1 := []int{1, 2, 3, 4} list_2 := []uint8{'a', 'b', 'c', 'd'} Map(list_1) Map(list_2) } // This function prints the every element for // all []types of array. func Map(list Iterable) { for _, value := range list { fmt.Print(value) } }
But it throws a compile-time error.
19: cannot range over list (type Iterable)
The error is correct, because range requires an array, a pointer to an array, slice, line, map or channel that allows receiving operations, and here it is Iterable . I think the problem I am facing is converting the type of the Iterable argument to an array type. Please suggest how I could use my function to process a shared array.
function arrays go
subhash kumar singh
source share