Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Arrays

An array is a data structure that consists of a collection of elements of a single type. An array in Go is a fixed-length data type that contains a contiguous block of elements of the same type. This could be a built-in type such as integers and strings, or it can be a struct type.

Declaration and Initialization

There are four common ways of declaring arrays.

1) A basic declaration of an array goes like this

[length]element_type

The type definition of an array is composed of its length, enclosed within brackets, followed by the type of its stored elements.

For example:

var intArray [5]int

The name of this array is intArray and the type structure is a [5]int. The type includes the length of the array(number of items it must contain). A string array with a length of 10 items is of type [10]string. In this example, the array is automatically populated by compiler, which assigns to each of the 5 index positions, a default value 0(int array type); space(string array type).

The default values - set when we declare an array - can then be reassigned manually to whatever data we want to have, as long as this data is of the intended type:

For example:

intArray[0] = 10

Index[0] assigns a value 10 to the first element of the array intArray. Like other programming languages, indexing in array starts with zero.

2) Declare an array with some or all values to it at the time of declaration

element_type{comma-separated list of element values}

The literal value for an array is composed of the array type definition followed by a set of comma-separated values, enclosed in curly brackets.

For example:

var intArray = [5]int{10,20,30}

In the above example, element[0] contains 10, element[1] contains 20 and remaining elements consist 0.

When arrays are initialized using an array literal, we have the option of customizing the index positions of the elements initially assigned to the array.

You can provide values for specific elements as shown here:

var intArray = [5]int{0:10,2:30,4:50}

Index element 0,2 and 4 were assigned to values 10,30 and 50; remaining values will be 0.

3) Declare an array inside function using the := shortcut

We can also declare array inside of a function using short variable declaration := like in the below example:

intArray := [5]int{10, 20, 30, 40, 50}

Index element 0,1...4 so on were assigned to values 10,20...50

4) Declaring an array with ellipses ...

The length of an array may be omitted and replaced by ellipses during initialization. Capacity is determined based on the number of values initialized.

The following will assign type [5]int to variable intArray:

intArray := [...]int{10, 20, 30, 40, 50}


Exercise

1) for loop to iterate over the Array

Create integer array named intArray, with a length of 5. Using a for loop to iterate over the array, display the contents of the array.

package main

import "fmt"

func main() {
	intArray := [5]int{10, 20, 30, 40, 50}
	
	for i := 0; i < len(intArray); i++ {
		fmt.Println(intArray[i])
	}
	
}
golang array

2) Assigning an array by value and reference

The values "Japan", "Australia", "Germany" are assigned to strArray1. strArray1 is then assigned to strArray2, which means that a copy of those values is created. The reassignment of the strArray1 to strArray3 from a value assignment to a reference assignment.

package main

import "fmt"

func main() {

	strArray1 := [3]string{"Japan", "Australia", "Germany"}
	fmt.Printf("strArray1: %v\n", strArray1)
	strArray2 := strArray1 // data is passed by value (copied)
	fmt.Printf("strArray2: %v\n", strArray2)
	strArray1[0] = "Canada"
	fmt.Printf("strArray1: %v\n", strArray1)
	fmt.Printf("strArray2: %v\n", strArray2)

	strArray3 := &strArray1
	fmt.Printf("strArray3: %v\n", strArray3)
	fmt.Printf("&strArray3: %v\n", &strArray3)
	fmt.Printf("*strArray3: %v\n", *strArray3)
	
}
golang array

strArray3 is a pointer string array, which means strArray3 is a pointer to the memory address of strArray1. In order to dereference strArray3, we have to add the * operator as a prefix *strArray3.

3) An example program that explores the array type

package main
import "fmt"

func main(){
	var x[5] int	// Array Declaration

	x[0]=10			// Assign the values to specific Index
	x[4]=20			// Assign Value to array index in any Order	
	x[1]=30
	x[3]=40
	x[2]=50	
	fmt.Println("Values of Array X: ",x)

	// Array Declartion and Intialization to specific Index
	y := [5]int{0:100,1:200,3:500}
	fmt.Println("Values of Array Y: ",y)


	// Array Declartion and Intialization
	Country := [5]string{"US","UK","Australia","Russia","Brazil"}
	fmt.Println("Values of Array Country: ",Country)

	// Array Declartion without length and Intialization
	Transport := [...]string{"Train","Bus","Plane","Car","Bike"}
	fmt.Println("Values of Array Transport: ",Transport)

}
golang array