Bob5421
Bob5421

Reputation: 9073

Is there a difference between slices and list?

Is there a difference in go between slices and list.

What I understood is there are array (fixed size) and slices (dynamic size), but is there a difference between these 2 things:

var slice []int
list := []string{"a", "b", "c", "d", "e"}

I would like to say they are both slices but am I correct?

Upvotes: 2

Views: 3911

Answers (2)

Aarthi
Aarthi

Reputation: 141

var slice []int
list := []string{"a", "b", "c", "d", "e"}

Both of these are slices.

It is an array only if size is mentioned in declaration. This size cannot be changed later, therefore it is of fixed size.

var array [5]int
array2 := [3]string{"a","b","c"}

Go Slices & Arrays

Understanding the difference between arrays and slices is easier when you look at their internal representations.

Array:

Array is a list of values laid out sequentially in memory.

array := [4]int{1,2,3,4}

       +-----------------------+
[4]int |  1  |  2  |  3  |  4  |
       +-----------------------+

Once you have defined an array, it's size cannot be changed. The [4]int here will always remain an array of 4 integers. [4]int and [5]int are incompatible types.

Slice:

The slice type is an abstraction built on top of Go's array type. It is a descriptor of a segment of an array.

It consists of 3 components

  1. ptr - a pointer to the underlying array,
  2. len - the length of the segment,
  3. cap - and, its capacity (the maximum length of the segment).

           +-----+
     []int | ptr |
           +-----+
           | len |
           +-----+
           | cap |
           +-----+
              |
              |
           +-----------------------+
    [4]int |  1  |  2  |  3  |  4  |
           +-----------------------+
    

The slice can be modified to point to a different array of same or different size. Hence, the slice has dynamic size.

 slice1 := []int{1,2,3} 

       +-----------------+
slice1 | ptr |  3  |  3  | 
       +-----------------+

 slice1  = []int{1,2}

       +-----------------+
slice1 | ptr |  2  |  2  | 
       +-----------------+

And, there can be more than one slice pointing to the same array.

Upvotes: 8

peterSO
peterSO

Reputation: 166569

The Go Programming Language Specification

Slice types

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The value of an uninitialized slice is nil.

SliceType = "[" "]" ElementType .

A Go slice is a slice of an underlying array. Go doesn't have lists. In Go, you can implement a list using a slice.

These are both Go slices. Neither is a list.

var intSlice []int
strSlice := []string{"a", "b", "c", "d", "e"}

intSlice is initialized to the zero value for a slice which is nil. The elements of the underlying array.for strSlice are initialized to the slice composite literal values {"a", "b", "c", "d", "e"}.

Upvotes: 1

Related Questions