编写对“通用”数组进行操作的方法的惯用方法是什么?
我有一个类型数组:
a := make([]int, 0)
我想编写一个可以对任何类型的数组进行操作的简单方法:
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
}
使用这种方法a = reverse(a)给我带来了两个错误:
cannot use a (type []int) as type []interface {} in argument to reverse
cannot use reverse(a) (type []interface {}) as type []int in assignment
料青山看我应如是
慕娘9325324
慕容708150
相关分类