dongzhun1857 2016-08-30 04:43
浏览 250
已采纳

如何使用默认排序功能在Golang中对整数数组进行排序

A stupid question. I can't sort using default sort function in go

    package main
    import "fmt"
    import "sort"
    func main(){
            var arr [5]int
            fmt.Println("Enter 5 elements")
            for i:=0;i<5;i++{
                    fmt.Scanf("%d",&arr[i])
            }
            sort.Ints(arr)
            fmt.Println(arr)
    }

When executing the above program, It throws out

cannot use arr (type [5]int) as type []int in argument to sort.Ints

Need Help.

  • 写回答

1条回答 默认 最新

  • duanbipu1720 2016-08-30 04:48
    关注

    sort.Ints expects a slice of int, not an array. Easiest fix is to change

    sort.Ints(arr)
    

    to

    sort.Ints(arr[:])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?