Solution 1:

See "Slices: usage and internals"

var av = []int{1,5,2,3,7}

That is a slice, not an array.

A slice literal is declared just like an array literal, except you leave out the element count.

That explains why the sort function will modify the content of what is referenced by the slice.

As commented below by Kirk, sort.Ints will give you an error if you passed it an array instead of a slice.

func Ints(a []int)

Solution 2:

Because you're using a slice, not an array.

That is a slice:

var av = []int{1,5,2,3,7}

And those are arrays:

var av = [...]int{1,5,2,3,7}
var bv = [5]int{1,5,2,3,7}

If you try to compile:

var av = [...]int{1,5,2,3,7}
fmt.Println(av)
sort.Ints(av)
fmt.Println(av)

, you will get an error:

cannot use av (type [5]int) as type []int in function argument

as sort.Ints expects to receive a slice []int.

Solution 3:

[]int{1,5,2,3,7} is not an array. An array has it's length in it's type, like [5]int{1,5,2,3,7}.

Make a copy of the slice and sort it instead:

a := []int{1,5,2,3,7}
sortedA := make([]int, len(a))
copy(sortedA, a)
sort.Ints(sortedA)
fmt.Println(a)
fmt.Println(sortedA)

Solution 4:

var av = []int{1,5,2,3,7}

in the above statement you are initializing slice like an array

To create an array the syntax should be

var av = [5]int{1,5,2,3,7}

Solution 5:

slices are pointer to array . when you copy an array to another or when you pass a array in the function the entire copy of array is copied or passed . This makes a costlier operation if thae array size is large. so we can go for slices.