Go Array project

The Go Array, What the Heck is a Slice?

 

We covered the basic data types of Go when we learned about variables, so now it’s time to introduce a new kind of variable, the Go Array. In programming, an Array is an indexed ordered list of values. In our introduction to variables, we used the analogy of a post office P.O. box to represent a variable. The P.O. box has an address, and it can hold a value.

You can think of an Array as being a row of P.O. boxes on the wall at the post office. We can refer to the row of P.O. boxes that we’re talking about (that’s the variable name) and which box in a row we want to look in. Each P.O. box in the row is still capable of holding a value.

Just like with other variables Go Arrays have a type assigned to them. Every value we place in the Array must be the same type as the Array itself.

Go Array Literals

Let’s see Arrays in action to help better understand the concept. We’ll start by creating what’s called an Array literal. A Go Array literal means that we’ll create the Array and assign values to it at the same time. Previously when we created a string and assigned a value to it at the same time that was creating a string literal.

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

import (
    "fmt"
)

func main () {
    var myArray = [5]string{
        "this",
        "is",
        "a",
        "go",
        "array",
    }

    fmt.Println(myArray)
}

Go Arrays have a fixed length. When you create the Array, you tell Go how many values the Array will hold Go will create the Array with exactly that many positions.

1
var myArray [5]string

We’re creating an Array named myArray, the brackets [ and ] tell Go that we’re creating an Array. The number inside the brackets specifies the size of the Array we want to create. The type that we want the Array to store comes after the brackets. So, in this case, we want to create an Array that can hold five strings.

To create our Array and assign values to it at the same time we use braces ({ and }) after the type. We will list out the values that we want to store in the Array, in order, separated by commas. We could have put all of the values on the same line, but with a long list of values, this can make the code hard to read. Instead, we put each value on a new line in our code. The critical thing to notice is that when you put each value on new lines as we did you need to have a comma after the last value. If you list the values on the same line, this final comma isn’t required.

Go Array Indexes

As we mentioned in our definition of a Go Array, an Array is an indexed ordered list. Revisiting our row of P.O. boxes analogy, the index is the number of each P.O. box in the row. In Go, like many other programming languages, we start counting Array positions at 0. So the first position of the Array is always position 0. If we want to print out just the value stored in the third position of our Array (the word “a”), we will use the index of 2, since we start at 0. You access the value of an Array position by using the index position inside of brackets after the Array name.

Go Array indexing

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

import (
    "fmt"
)

func main () {
    var myArray = [5]string{
        "this",
        "is",
        "a",
        "go",
        "array",
    }

    fmt.Println(myArray[2])
}

Go Array Length

It’s important never to try to access a position of your Array using an index that does not exist. If you attempt to use an index that’s larger than your array your program will crash with what’s known as an index out of bounds error. Conveniently Go provides a method for checking the length of an Array. You must include checks in your code against the length of an Array before trying to access a position you don’t know exists.

1
2
3
4
5
6
7
8
9
10
11
var myArray [5]string

var i = 5

fmt.Println(len(myArray))

if len(myArray) > i {
    fmt.Println(myArray[i])
} else {
    fmt.Println("myArray doesn't have a position", i)
}

The len() function returns the number of positions in our Go Array. Because array indexes start at zero, the length of the Array will always be the highest index plus one. To make sure that an Array index we want to access exists we can check to make sure that the length of the Array is larger than the index we’re trying to use.

So What the Heck is a Slice?

Go Arrays are a useful data type, but you’ll more commonly use them as a building block for creating a new type instead of using them directly. For situations where you want to use an ordered indexed list of values more commonly, you’ll use a Slice instead of an Array.

A Slice is functionally equivalent to an Array in how you use it, with one crucial difference. An Array has a fixed size that you set when you create it, and its size can’t change once you’ve created it. A Slice, however, can be expanded to hold more values or shrunk to hold fewer values.

Behind the scenes, a Slice contains a Go Array that the Slice uses to hold its values. When you try to store a new value in the Slice and its Array is too small the hold the value the Slice will automatically create a larger Array, copy the current values into the new Array, and add the new value to the end of the Array.

Go Array Slice

1
2
3
4
5
6
var mySlice = []string{
    "hello",
    "world!",
}

fmt.Println(mySlice)

Creating and using a Slice is nearly identical to creating and using a Go Array. The only difference is that to create a Slice you don’t specify a size.

Creating Slices from Go Arrays and Other Slices

So far we’ve only accessed Go Array and Slices elements using a single index. What do you do if you want to obtain a whole portion of an Array or Slice instead of just one index? Go allows you to use the colon (:) to access a range of Go Array or Slice indexes to create a new Slice from that range.

Go Array Slice operations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var myArray = [5]string{
    "this",
    "is",
    "a",
    "go",
    "array",
}

fmt.Println(myArray)

slice1 := myArray[2:3]
slice2 := myArray[:2]
slice3 := myArray[3:]
slice4 := myArray[:]

fmt.Println(slice1)
fmt.Println(slice2)
fmt.Println(slice3)
fmt.Println(slice4)

Using the colon to access the index of a Go Array or Slice returns a Slice starting at and including the value at the index on the left and up to but not including the value at the index on the right. When using the colon you can exclude the left, right, or both values. Leaving out the left value returns a slice from the 0 index. Leaving off the right value returns a slice from the left index until and including the last index. Leaving off both the left and right values returns a slice containing all of the index positions.

Modifying Go Array and Slice Values

We’ve talked extensively about indexes on Go Arrays and Slices now, but we’ve so far only been retrieving values from them. Once you’re comfortable with indexes updating a Go Array or Slice is easy.

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

import (
    "fmt"
)

func main () {
    var mySlice = []string{
        "this",
        "is",
        "a",
        "go",
        "slice",
    }

    mySlice[3] = "golang"
    fmt.Println(mySlice)

    mySlice = append(mySlice, "with another element")

    fmt.Println(mySlice)
}

To change the value of an existing index of a Go Array or Slice we assign a new value to that index position. Go provides the append function for adding values to a Slice. Append does not modify the Slice, it instead returns a Slice which contains the original Slice with the new value appended to it. To update the existing Slice, you must assign the result of the append function to the value of the slice.

 

Chris Sotherden

Originally a native of upstate NY, I relocated to North Carolina in 2013 where I've since helped open the new R&D headquarters for Optanix. I've been with Optanix (formerly ShoreGroup) for over ten years, where I've worked my way up from entry-level to Lead Architect. I taught myself programming in high school and am an all around technology nerd from the design of programming languages and compilers, to quantum computing, and rockets. Outside of technology I've competed in powerlifting and medium distance running, as well as smoking some mean BBQ. Oh, my partner and I also breed, show, and sell fancy goldfish!

>