今天写代码的时候相对一个定长数组进行排序,但是使用sort发现会报错

1
2
3
// A code block
sort.Sort(sort.IntSlice(a)
sort.Ints(a)
1
2
3
4
5
6
7
8
9
// An highlighted block
    var a [10]int
    //b := []int{3, 5, 4, -1, 9, 11, -14}
    for i := 0 ; i<10 ;i++{
        fmt.Scanf("%d",&a[i])
    }
    var tmp int = 10
    sort.Sort(sort.IntSlice(a)
    sort.Ints(a)

这样写是会报错的:cannot use a (type [10]int) as type []int in argument to sort.Ints
但是为什么会怎样呢,我查询有关golang的“sort”包的使用,都是一些类似于:对于b := []int{3, 5, 4, -1, 9, 11, -14}进行sort.Sort(sort.IntSlice(b)操作,但是定长声明的数组就不行。
解决方案:

1
2
sort.Sort(sort.IntSlice(a[:]))
sort.Ints(a[:])

这两种都可以,也可以通过:切片来进行部分排序。