What's the idiomatic way to write a method that operates on a "generic" array?
I have a typed array:
a := make([]int, 0)
I want to write a simple method that could operate on an array of any type:
func reverse(a []interface{}) []interface{} {
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
return a
}
Using this method a = reverse(a)
gives me 2 errors:
cannot use a (type []int) as type []interface {} in argument to reverse
cannot use reverse(a) (type []interface {}) as type []int in assignment