Sunday, April 12, 2015

Go - Arrays with Code Examples

An array is a group of related data items (collection of similar items) that share a common name. 


Related data items = Items with same data type

Array Declaration
var studentName [3] string


The above variable named studentName represents an array of type string that can hold up to 3 student names.

var studentMarks [3] int

The above variable named studentMarks represents an array of type integers that can hold up to 3 integer values. Gaphically the declaration var studentMarks [3] int can be simplified as:

Assigning Values to Array


studentMarks[0] = 80
studentMarks[1] = 75
studentMarks[2] = 64


As we've specified the size (length) of the studentMarks variable to 3; it can't take more than 3 elements. Here it is important to note that Arrays are indexed starting from 0. So, studentMarks[0] = 80 is like telling the compiler to assign the value 80 to the 1st element of the array studentMarks. This can be graphically represented as:

Remember Go Arrays are value types i.e. if you assign one array to another you get a copy of the array because value type variables are created on the stack.

Code Examples


package main
import "fmt"
func main(){

 var studentMarks [3] int
 
 studentMarks[0] = 80
 studentMarks[1] = 75
 studentMarks[2] = 64
 
 //2nd element of the array
 fmt.Println(studentMarks[1])
 
 //All the elements of the array
 fmt.Println(studentMarks)
 
 //Lenth of the array 
 fmt.Println(len(studentMarks))
 
 //Calculate average marks 
 fmt.Println((studentMarks[0]+studentMarks[1]+studentMarks[2])/len(studentMarks))
}


The output of the above code is shown in the below image.
  • You can play & see the output of the above code at Go Playground
Now let us see how an experienced professional will write the Average Marks Calculation part of the above code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main
import "fmt"
func main(){
  
     studentMarks := [3]int {80, 75, 64}  
     var totalMarks int
 
 //Calculate average marks
 
     for i := 0; i < len(studentMarks); i++ {  
     totalMarks += studentMarks[i]
 }
 
     fmt.Println(totalMarks/len(studentMarks))
}

  • You can play & see the output of the above code at Go Playground

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main
import "fmt"
func main(){
  
    studentMarks := [3]int {
     80, 
     75, 
     64, //Note the comma here in this line
   }  
   var totalMarks int
  
 /* Average marks using range keyword.
 Special FOR LOOP: A _ (underscore, known as blank identifier
 used below tells the compiler 
 that we don't need the iterator variable */
 
  for _, value := range studentMarks {    
 
  totalMarks += value
 }
 fmt.Println(totalMarks/len(studentMarks))
}



  • You can play & see the output of the above code at Go Playground
Question

What is the output of the following code?

1
2
3
4
5
6
7
8
package main
import "fmt"
func main(){

 var studentMarks [1] int 
 fmt.Println(studentMarks[0])
 
}

Take a guess.

Answer Clue

In Go, if we just declare a variable and do not initialize it, by default, Go assigns a zero value to the variable. Depending on the type:
  • An Integer variable is assigned 0, 
  • Boolean is assigned false, 
  • String is assigned with ""  i.e. empty space.

Practical Implementation

Arrays are a bit rigid as they are fixed length and can't grow dynamically in size. So, although there usage during practical programming is very limited the concept of Arrays must be understood to form a good foundation of what we're going to learn next i.e. Slices.

Feel free to comment and share.


No comments:

Post a Comment