Go/Golang - Data Structures ( Arrays,Slices and Maps)

Go/Golang - Data Structures ( Arrays,Slices and Maps)

ยท

3 min read

Arrays

An Array is a collection/sequence of values of a single type. Arrays in Go are similar to arrays in other languages. Let's see how to declare an array in Go, Below is an example:

var value1 [5]int

var value2 [3]string

You can also initialize arrays with pre-defined values.

var value1 = [5]int { 11, 22, 33, 44, 55}

value2 := [3]string{ "This is " ,"Golang" , "Blog series"}
//Initializing with short variable declaration operator

You can access all the elements of an array using for loop. Below is a code snippet :

    var numArr = [5] int {24,76,45,33,65}

    for i:=0; i< len(numArr); i++ {
                   fmt.Println("Value is :",numArr[i])
    }

Thelen() function above is used to find the arrays's size.

Note: Arrays are passed as values to a function. That is we pass a copy of it.

Slices

A Slice in Go is very similar to an array. Arrays are of fixed size, But slices are dynamically sized. Slices can double their current size to add more values. Slices store elements of similar type.

Slices are declared just as an array but they don't specify the size. Below is an example of that:

var sliceValues1 [ ]int 

var sliceValues2 [ ]string

To add values to the slice we use append() function. Let's see a code snippet where we initialize a slice and add more values to it.


var intSlice = []int {1,2,3,4,5,6,7}
intSlice = append(intSlice,8)

fmt.Println("Length : ", len(intSlice)) /*Prints length of slice*/
fmt.Println("Capacity :" ,cap(intSlice)) /*Prints capacity of slice*/

intSlice = append(intSlice,9,10,11) //You can append multiple values too
fmt.Println(intSlice) //Prints slice after appending multiple values

In the above snippet len() gives us the current length of slice cap() gives us the total capacity of it.

We can create a slice of a slice by using the below code :

sliceVal := []int{10,20,30,40,50,60}

sliceVal1 := sliceVal[0:4] // It creates a slice of 10,20,30,40 

fmt.Println(sliceVal1)

One important note is that Slices are passed by reference in Go.

Maps

Maps in Go are used to store key-value pairs and It doesn't allow any identical keys. Let's see how to declare a map in a code snippet below:

var animals map[int]string

In the above snippet, The key is of type int and the value is of string type.

You have to initialize a map when declaring it or else it's a nil Map and you can't add elements to a nil Map. Let's see how to initialize a Map in Go.

var animals map[int]string {
       1 : "Tiger" ,
       2 : "Lion" ,
       3 : "Elephant" ,
       4 : "Giraffe", //The comma is a must after the last key
}

A Map can be created using make() function. make() function creates an initialized map, Below is a code snippet to create that:


prices := make(map[string]int)
prices["Item1"] = 240
prices["Item2"] = 350
prices["Item3"] = 150

The above prices map can be iterated as follows:

for item, price := range prices {
            fmt.Println(item , ":>" ,price)
}

You can remove a key using the built-in delete() function

delete(prices, "Item1")

A Point to Note is Maps are reference types. So when a Map is assigned to a variable, If any changes are made in the variable it also changes the Map.

That's it for this blog โœ….

For more information, You can always check out the official documentation. This is the official site ๐Ÿ’ฏ.

If you are new to the Go language and you want to check out more, Below are some resources ๐Ÿ”ฅ.

Resources

  • Free coding exercises(Gophercises) course in Go by John Calhoun. You can check it out here.

  • A full video course on Go by freecodecamp.

  • There's a course on Codecademy on Go language. You can check it out here.