58

I found myself confused with the array and slice data types.

From Go docs, arrays are described as follows:

There are major differences between the ways arrays work in Go and C. In Go,

  • Arrays are values. Assigning one array to another copies all the elements.
  • In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
  • The size of an array is part of its type. The types [10]int and [20]int are distinct.

Functions:

As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to.

Why does sort.Ints(arrayValue) modify the passed variable when I declared it as an array, not as a slice?

Code

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

fmt.Println(av)
sort.Ints(av)
fmt.Println(av)
return

Output

[1 5 2 3 7]
[1 2 3 5 7]
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
duganets
  • 1,853
  • 5
  • 20
  • 31

5 Answers5

55

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.

Artem Shitov
  • 2,889
  • 1
  • 15
  • 8
55

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)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    In addition, sort.Ints will give you an error if you passed it an array instead of a slice. Documentation for sort.Ints specifies a slice. https://golang.org/pkg/sort/#Ints – Kirk Apr 29 '15 at 18:37
  • @Kirk good point. I have included it in the answer for more visibility. – VonC Apr 29 '15 at 18:41
11

[]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)
Zippo
  • 15,850
  • 10
  • 60
  • 58
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.

swarna
  • 51
  • 1
  • 1
3
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}
user
  • 2,694
  • 6
  • 24
  • 25