Arrays in Go Programming

Arrays in Go Programming

Basics of arrays in Go programming language

ยท

3 min read

๐Ÿ‘‹ Hi everyone and welcome...

In this article i'll explain arrays in go programming, let's start

Array.jpg

๐Ÿ’ก What is an Array?

Imagine that you have a library that contains multiple sections, let us call these sections rows and columns and you use these sections to store different types of books ( Historical, Science, etc) this is arrays in real life.

๐Ÿ’ก Arrays in Go programming language

If we need to store a collection of data of the same type, like a list of color names. Such type of collection is stored in a program using an Array. An array is a fixed-length sequence that is used to store homogeneous elements in the memory.

Code-6.png

๐Ÿ’ก You can create

  • One-dimensional array.
  • Multidimensional arrays.

๐Ÿ’ก What is a one-dimensional array?

You can think of a one-dimensional array as a row, where elements are stored one after another in a single row and multiple columns.

๐Ÿ’ก What is a multidimensional array?

Multidimensional arrays in simple words as an array of arrays (multiple rows and columns).

๐Ÿ’ก What is an array index?

Is the identity (Location) of stored data in your array always start from zero 0.

๐Ÿ’ก To create Arrays

var fruits [3]string    // One Dimensional Array
var colors [2][3]string // Multidimensional Array

๐Ÿ’ก To initialize an arrays

package main

import (
    "fmt"
)

func main() {

    var fruits [3]string    // One Dimensional Array
    var colors [2][3]string // Multidimensional Array

    fruits[0] = "Lemon"
    fruits[1] = "Avocado"
    fruits[2] = "Grape"

    fmt.Println("One Dimensional Array:", fruits)

    colors[0][0] = "Purple"
    colors[0][1] = "Red"
    colors[0][2] = "Green"
    colors[1][0] = "Yellow"
    colors[1][1] = "Black"
    colors[1][2] = "Orange"

    fmt.Println("Multidimensional Array:", colors)
}

// Output
// One Dimensional Array: [Lemon Avocado Grape]
// Multidimensional Array: [[Purple Red Green] [Yellow Black Orange]]

๐Ÿ’ก Length of Arrays

To get length of arrays, strings, slices and maps use len() method, it start counting from one.

fmt.Println("Length of One Dimensional Array:", len(fruits))
fmt.Println("Length of Multidimensional Array:", len(colors))

๐Ÿ’ก To print first item in One Dimensional and Multidimensional Array

fmt.Println("First Item:", fruits[0])
fmt.Println("First Item:", colors[0][0])

๐Ÿ’ก To print first row in Multidimensional Array

fmt.Println("First Row:", colors[0])

๐Ÿ’ก Use for loops to Print Arrays

for item := 0; item < len(fruits); item++ {
        fmt.Printf("Fruits %d : %s |", item, fruits[item])
}

//---------------------------------//

for row := 0; row < 2; row++ {
    for column := 0; column < 3; column++ {
        fmt.Println(colors[row][column])
    }
}

//---------------------------------//

for row := 0; row < len(colors); row++ {
    fmt.Printf("Row %d: ", row)

    for column := 0; column < len(colors[row]); column++ {
        fmt.Printf("%s, ", colors[row][column])
    }

    fmt.Println("")
}

๐Ÿ’ก range()

range() Method helps you to loop over elements of array and Print them.

for id, value := range fruits {
    fmt.Println(id, "->", value)
}

//---------------------------------//

for index, value := range colors {
    fmt.Println(index, "->", value)

    for _, valInside := range colors[index] {
        fmt.Printf("%s ", valInside)
    }

    fmt.Println("")
}

That's it for today ...

I hope you gained something here today and reading it was worth the time.

Stay tuned for next articles Good Luck ๐Ÿ˜Š.

ย