27

I have read this but still not fully aware of the advantage of slice against array.So I am expecting somebody in SO explain better than it and I am sure you can :)

4 Answers 4

25

Slices have a lot of uses over arrays, several of which other posters have already mentioned.

  • Slices can operate in many ways like pointers.
    • Multiple slices can "point" to the same base array
    • Slices are passed by reference, but since a slice itself is a pointer it can be used to pass "arrays" around much more efficiently since the entire array doesn't need to be copied.
  • However, unlike pointers, slices provide additional buffer safety
    • Slice underflows and overflows trigger exceptions, rather than allowing you an unsafe ability to access other areas of memory.
    • Slices allow you to limit access to only certain areas of an array. This can be extremely useful in working with subsets.
  • The length of a slice is dynamically determined at runtime, unlike arrays which have their sizes fixed at compile time. Also, slices can be dynamically resized at runtime.
1
  • 3
    Slices are not passed by reference, they're copied. A slice itself is not a pointer and they don't pass arrays. A slice value is passed by copy as everything in Go. Only that, the passed slice value "contains" a pointer to a backing array (slice value is not a pointer itself). Dec 28, 2018 at 20:38
15

In go, arrays are passed by value; so, to "pass by reference", you use a slice. And that's not all! Quoting Go's tutorial:

The size of the array is part of its type; however, one can declare a slice variable, to which one can assign a pointer to any array with the same element type or—much more commonly—a slice expression of the form a[low : high], representing the subarray indexed by low through high-1. Slices look a lot like arrays but have no explicit size ([] vs. [10]) and they reference a segment of an underlying, often anonymous, regular array. Multiple slices can share data if they represent pieces of the same array; multiple arrays can never share data.

Slices are much more common in Go programs than regular arrays; they're more flexible, have reference semantics, and are efficient. What they lack is the precise control of storage layout of a regular array; if you want to have a hundred elements of an array stored within your structure, you should use a regular array.

When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.

2
  • Be careful when you append something to a slice, you created a new reference: a = append(a, something). The examples at this link may explain further: play.golang.org/p/5c4PyzDOzb
    – user1663023
    Aug 23, 2014 at 10:14
  • Elgs: That doesn't create a new "reference". There are no reference values in Go. Append creates a new backing array only when the existing backing array doesn't have enough capacity to hold the new element(s). Dec 28, 2018 at 20:37
2

I think slices and arrays are described much better and in more detail in this post on the Go Blog.

1

In addition to the answers already given, slices can be dynamically sized while arrays cannot be. That is, you can only use constants to specify the size of an array, while you can use a variable to specify the size of a slice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.