Slices in Go

Let's learn about Slices....

ยท

3 min read

Slices in Go

Greetings Folks,

Slices are a reference to an array, providing more flexibility for the developers to work with these data structures. This allows you to work with a portion of an array as if it were an independent array. Arrays have a fixed size while slices are dynamic in size, through which the size can be increased or decreased as needed.

We can create Slices by 3 methods:

  • Using the []datatype {...}

  • From an Array

  • Using make()

We can form a slice by:

a[low : high]

Now this slice will cover elements from a[low] to a[high -1] elements. To get a better understanding of this let's see the example below

func main(){
    example:= [4]string{"aa", "bb", "cc", "dd"}
    var a []string = example[1:3]
    fmt.Println(a)
    fmt.Println(example[2:4])
}

Output:

[bb cc] 
[cc dd]

In the above example, we have covered the first 2 ways of creating a slice and we can see that in var a, elements that are being covered are 1st index(considering 0 indexing) and 2nd index elements.

If the elements in a slice are modified, the change is reflected in the respective element of the corresponding array.

func main() {
    samples:= [4]string{"Dennis", "Paul", "Quentin", "Ari"}
    fmt.Println("Before changing:", samples)

    a := samples[0:3]
    a[2] = "Martin"    // Modifying the 3rd element

    fmt.Println("After changing:",samples)
}

Output:

Before changing: [Dennis Paul Quentin Ari]
After changing: [Dennis Paul Martin Ari]

Now, a slice has both a length and capacity denoted by len() and cap().

Main difference between length and capacity is that by capacity we are counting the no. of elements from the first element, while length gives us the no. of elements a slice contains. Let's check out an example to understand this better...

func main() {
    example:= []int {1,2,3,4,5,6,7}
    var a[]int = example[0:4]

    fmt.Println("Capacity:", cap(a))
    fmt.Println("Length:", len(a))
}

Output:

Capacity: 7
Length: 4

Slices can also be created using the built-in make() function.

General form of make() function is:

s:= make([]int, length, capacity)
a:= make([]int, length)    // length and capacity will be same

To understand the working of make(), let's check out the snippet below

func main(){
    s:= make([]int, 4, 6)
    for i:= 0; i < 4; i++{    // Populating 
        s[i] = i * 2
    }

    fmt.Println(s)
    fmt.Println(len(s))
    fmt.Println(cap(s))
}

Output:

[0 2 4 6]
4
6

We can also append elements to a slice using append(s[]T, ...)

Now let's see something interesting ๐Ÿ‘€

func main() {
    var a =[]int {1,2,3,4,5,6}
    a = append(a, 7,8,9)    // we can add 1 or more elements

    fmt.Println("Length:",len(a))
    fmt.Println("Capacity:",cap(a))
    fmt.Println(a)
}

Output:

Length: 9
Capacity: 12       
[1 2 3 4 5 6 7 8 9]

If you notice in the output, the capacity has become 12 for some reason but it should be 9, what is the reason behind this behavior ๐Ÿค”

Let's go back to the basics for this, an array is a fixed-size data structure and we have already declared a size of 6 for it but when we append 3 elements to the array, Go creates another array by doubling the capacity because of which we get the capacity 12.

And that is all for the blog, I hope you learned something new from this ๐Ÿ˜. Would love some feedback from y"all :)

Thank You

ย