Monday, April 20, 2015

Go Slices with Code Sample

If you are a newcomer, it is recommended that you read the previous post about Go - Arrays with Code Examples to fully understand Slices.

Slices?

Simple Arrays are fixed length and are not so flexible to use when it comes to practical programming scenarios. A Slice is a segment of an underlying array that can vary in length as per our requirement. In  plain English:




Slice = Variable length array  

Remember

  1. The length of a slice may be changed as long as it still fits within the limits of the underlying array.
  2. Slices are reference type - the variable are stored in the heap

Creating Slices

var studentAge []int

The above line of code declares a slice variable named studentAge. How's it different from a simple Array? Did you notice the missing length between the brackets? Yes, that's the only difference in terms of declaration. You can visit this post about Simple Go Arrays to see how we declared an Array.

Standard way to create a Slice is by using built-in make function:


studentAge := make([]int, 5)

The above statement creates a Slice with an initial length of 5 that is part of an underlying array of capacity  (maximum length) 5. 

A 3rd parameter which is optional can also be added to the make function:

studentAge := make([]int, 3, 5)

The above represents a Slice of length 3 that points to an underlying array of capacity (maximum length) 5. Graphically:

Following is shorthand version of assigning values to Slices 

studentAge := []int{12,15,11,13,10}


Changing the Length of a Slice - Reslicing

newAge := studentAge[0:2]

Here we are changing the length of studentAge slice. Where to start slicing and where to end slicing in the underlying array? (Note: underlying array has values {12,15,11,13,10}) 

                  0 is starting index from where we'll START slicing.
                  2 is the end index where we'll STOP slicing. [excluded - i.e. you must stop just before this index]

Try and execute the following code to fully understand what we mean by reslicing. Play here.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import "fmt"

func main() {
    studentAge := []int{12,15,11,13,10}
    fmt.Println("Students Age ==", studentAge)
 
 //studentAge[0:1] means Slice at start_index 0 & end_index 1
 //If 'start_index' is not mentioned, by default it is 0
 //If 'end_index'  is not mentioned, by default it is the max length of underlying array
 
    fmt.Println(studentAge[0:1]) 
    fmt.Println(studentAge[0:2])
    fmt.Println(studentAge[1:2])
    fmt.Println(studentAge[1:3])
    fmt.Println(studentAge[0:0])
    fmt.Println(studentAge[0:])
    fmt.Println(studentAge[:3])
    fmt.Println(studentAge[:])
}

The output of the above snippet:
Hope the above makes sense. Please add your comment to let me know if I missed something in the description. Let us make these tutorials Go newcomer friendly. In the next post we'll learn about Adding Elements to a Slice.

No comments:

Post a Comment