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]
The basic difference between a slice and an array is that a slice is a reference to a contiguous segment of an array. Unlike an array, which is a value-type, slice is a reference type. A slice can be a complete array or a part of an array, indicated by the start and end index.
slice is a composite data type and because it is composed of primitive data type (see variables lesson for primitive data types). Syntax to define a slice is pretty similar to that of an array but without specifying the elements count. Hence s is a slice.
JavaScript Array slice()The slice() method returns selected elements in an array, as a new array. The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.
Common examples of array slicing are extracting a substring from a string of characters, the "ell" in "hello", extracting a row or column from a two-dimensional array, or extracting a vector from a matrix. Depending on the programming language, an array slice can be made out of non-consecutive elements.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With