What is a Go array
Array in Golang is a data structure that is used to store a collection of elements of same data type. An array is of a fixed size or length which is declared at the time the array is declared or initialized.

Array declaration
A Golang array is also a variable and is declared in the same way other variables are declared. Syntax of array declaration is

var array_name [size] data_type

where,
var is the keyword to declare a variable.
array_name is a user defined name which shall be used to access array elements.
size is an integer representing the total(or maximum) number of elements that the array will contain. Size should be enclosed between square brackets.
Size of array cannot be changed after array declaration.
data_type is the type of values that the array will contain.
Array declaration examples
Below are the valid examples of declaring an array.

// string array of 5 elements
var names [5] string

// integer array of 5 elements
var numbers[5] int16

// float array with 5 elements

var rates [5] float32

It is also possible to provide the size of the array as a Go constant or an expression. Example,

// declare a variable with value 5
const size = 5
// declare an array of 5 strings
var names[size] string

// declare two constants
const min =0
const max = 5
// declare array with size as expression
var numbers[min + max] int32

Size of array should be declared at the time of its creation, the size can be also provided as a constant value since it will not change once defined.

Array allocation
Array elements in Golang are stored in contiguous memory locations means all array elements are stored one after other and not randomly distributed in memory.
An array in Golang will be represented in memory as below.
Golang array allocation in memoryNotice that the array elements are stored in adjacent memory addresses.
Accessing array elements
An array can be referenced using the name of its variable. Individual array elements are accessed using their index enclosed in square brackets.
Index of array elements is numeric with the first element at index 0, second at index 1 till the last element whose index is one less than the total number of elements in array.
Thus, syntax to access elements of an array is

array_variable[index_of_element]

Initializing array
Initializing array means providing value to its elements. There are two ways of setting values of array elements.
1. At the time of array creation
Initialize array elements at the time array is declared by writing values in curly braces as below.

var numbers = [5] int{1, 2, 3, 4, 5}

Note that this way, the size and data type are to the right of assignment operator(=).
If you initialize elements at the time of declaring the array, then the size can be omitted since the compiler can interpret the total elements from values.
Thus, above declaration can also be written as

var numbers = []int{1, 2, 3, 4, 5}

There is a short hand notation for initializing array which does not require var keyword as below.

numbers := [5]int{1, 2, 3, 4, 5}

Again you can omit the size here as well since the compiler can figure it out from the number of elements.

numbers := []int{1, 2, 3, 4 ,5} 

2. Initializing individual elements
Access each array element using its index and assign it a value as below.

var numbers [5] int
numbers[0] = 10 
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

This method is not feasible for large arrays so you can use a for loop to initialize elements as below.

var numbers[] int
for i:=0; i < 5; i++ {
   numbers[i] = i
}

Getting values of elements
Array elements can be accessed using numeric indexes. Array variable followed by the index of an element will return the value stored at that index.
This value can be used directly in an expression or assigned to a variable of same type as the array. Example,

var num1 = 10
// assigning array element to a variable
var num2 = numbers[0]
// using array element in expression
var sum = num1 + num2 + numbers[1]

If you try to assign an element from int array to a string variable, then you will get an error
cannot use arr[0] (type int) as type string in assignment
Modifying array elements
It is possible to change or assign value to an array element after the array is created. Write array variable and index of element at the left and the value to assign at the right of assignment operator(=). Example,

numbers := []int{10, 12, 30}
// update second element
numbers[1] = 20

Again, the type of value assigned should match the type of array.

Array length
Length of an array is the count of elements present in the array. Length is also referred as the size of array. It is an integer value and to know the length of array, use len function providing it the array as argument. Example,

package main

import "fmt"

func main() {
   var arr [5]int
   fmt.Println("Count of elements of array is ", len(arr))
}

which prints

Count of elements of array is 5

0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.