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

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.

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

  • One-dimensional array.
  • Multidimensional arrays.

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.

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

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

var fruits [3]string    // One Dimensional Array
var colors [2][3]string // Multidimensional Array
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]]

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))
fmt.Println("First Item:", fruits[0])
fmt.Println("First Item:", colors[0][0])
fmt.Println("First Row:", colors[0])
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() 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 ๐Ÿ˜Š.