Photo by Rustic Vegan on Unsplash

What is an Array?

An Array Data Structure is a collection of similar type of elements arranged in a serial manner.

Let's understand the above definition of Array by taking an analogy. Suppose you have 5 crayons, Since you like drawing things a lot, you want to carry it everywhere you go so you get a box. In order to place the crayons, you open the box and place all of them side by side, one by one. The box here is like an array, it is containing a similar set of items( crayons) in an organized way or in a serial manner. Now, suppose, this box is magical so you can ask for a crayon by using your voice. But the box is not that smart so in order to ask for a crayon you need to tell it the crayon’s position respective to the starting position.

Let’s see

//suppose you stored your crayons in following order

["Blue", "Green", "Red", "Purple", "Yellow"]

Now you want the “Red” color. Red is the third element so you will say give me the third element and the box will give you Red crayon. This is called indexing an array and the number you specified is called index. Each element has an index with which it can be accessed. The index is simply how many places away it is from the starting point. That was a lot of theory now let's try to implement them in the Go language.

Implementing Array

To use an array the first step is to declare it. We want the program to know that we need an array. Just open https://play.golang.org/ and try things out as explained in the following section.

Declaration

By writing the above line you just declared the array. The size is required beforehand to allocate the memory. We have declared an array but we have not specified what values it holds. It currently holds the default value also called Zero Value. the Zero value of the string is “”(empty string). The array holds ["" "" "" "" "" ] . Try printing out the array with fmt.Println(crayons)

Now let’s insert values into the array. Putting values is very simple. As explained in the above section, first you need an index (no. of places from starting position) and then you can set a value on that index.

crayons[0]="Blue"

Here we are saying the index is 0 ( Since this is the first element it is 0 no of places away from the first element. A simple way to get index is x-1 if its Xth element, it is called Zero Indexing). In order to set the value, we use the assignment operator(=) and specify the value. let’s do this for other colors as well. It is just like putting a crayon in a box, one by one.

crayons[1]=”Green”

crayons[2]=”Red”

crayons[3]=”Purple”

crayons[4]=”Yellow”

If you print with fmt.Println(crayons) you will get

There is another way of initializing the array



crayons := [5]string{ "Blue", "Green", "Red", "Purple", "Yellow" }

fmt.Println(crayons)



we use := to say that we are declaring as well as initializing the array and we pass the values of the array inside {} to initialize it.

Arrays On Playground

Now suppose you got two more crayons and you want to keep them inside the array as well but you can’t since you have previously declared that you only need to store 5 value. How would you store these additional crayons then? Enters Slices 😎

Slices

Arrays are good but they are not flexible, you can’t increase or decrease there size once declared. They take fixed memory space. In order to have a more flexible data structure, we have Slices in Go. Slices are just like Array but they are flexible enough.

In fact, Slices are built on top of arrays. A Slice has three main components

Array: Each slice holds an array internally. Actual data is stored in this array only Length: A Slice has Length property which specifies no of the element in it. Capacity: Slice holds the maximum length of that slice once the length reaches the capacity it reallocates memory and increases its capacity.

You declare a slice the same way you declare an array, the only difference is, since the slices are dynamically-sized you don't need to pass the size.

//no size specified therefore this is a slice

var crayons []string;

You will initialize it the same way you initialize an array

//without the size

crayons := []string{ "Blue", "Green", "Red", "Purple", "Yellow" }

fmt.Println(crayons)

You can index it also, in the same way, you will index an array given the index specifies is less than the length of the array i.e if the length of a slice is 4 you can’t index the seventh element (arr[7]).

There is a set of built-in methods that can aid in manipulating slices. let’s look at them

make: make initializes a slice it can take the length of the slice and we can pass capacity as well.

//len

crayons := make([]string , 7 ) //both len and cap

crayons := make([]string , 7 , 10)

2. copy: Copies a slice to another variable

crayonscopy := make([]int, 5) //crayonscopy = [] copy(crayonscopy, crayons)

//crayonscopy => ["Blue", "Green", "Red", "Purple", "Yellow"]

3. append: adds the element to the end of the slice

append( crayons, "Magenta")

//["Blue", "Green", "Red", "Purple", "Yellow","Magenta"] //can take multiple elements as well

append( crayons, "Brown" , "Pink")

//["Blue", "Green", "Red", "Purple", "Yellow","Magenta", "Brown" , "Pink"]

4. Slicing: slicing refers to taking a slice(verb) from the slice(data structure) like you can take a pizza slice. In order to take a slice(verb), we can do arr[x:y] where x and y are less than equal to the length and x <=y.

sliced1 := crayons[1:2] //take from 1 index upto index less than 2

//["Green"]



sliced2 := crayons[2:5] //take from 2 index upto index less than 5

//["Red", "Purple", "Yellow"]



sliced3 := crayons[2:] //take from 2 index upto end

//[Red", "Purple", "Yellow", "Magenta", "Brown" , "Pink"]



sliced2 := crayons[:3] //take from starting index upto index less than 3

//["Blue", "Green", "Red"]

So with this, we have covered slices as well. I highly recommend trying arrays and slices on the playground.