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.

Declaring and Initializing Arrays

Here is the code block that declares an array:

var test [4]int

A variable test is declared as an array of four elements of int type. Array test allows you to store four elements of integer values. You assign values to an array by specifying the index, which starts from 0. Here is the expression that assigns a value to the first element of array test :

test[0]=50

The expression test[3]=25 assigns a value to the last element (fourth element) of the array test. Once an array is declared, neither the type of data being stored nor its length can be changed. If you need more elements, you need to create a new array with the length needed and then copy the values from one array to the other.

// Declare an integer array of five elements.
// Initialize each element with a specific value.
test := [5]int{10, 20, 30, 40, 50}

If the length is given as ..., Go will identify the length of the array based on the number of elements that are initialized.

// Declare an integer array.
// Initialize each element with a specific value.
// Capacity is determined based on the number of values initialized.
test := [...]int{10, 20, 30, 40, 50}

If you know the length of the array you need, but are only ready to initialize specific elements, you can use this syntax.

// Declare an integer array of five elements.
// Initialize index 1 and 2 with specific values.
// The rest of the elements contain their zero value.
test := [5]int{0: 10, 1: 20}

An array of int type is declared with five as the size, but provides values only for the first element (index 1) and the secong element (index 2). You will get default values for the elements that didn't get initialized. For string type it is empty string, for integer type it is 0, and for boolean type it is false.

You can provide the initialization expression in a multiline statement as shown here:

test := [5]int {
5,
10,
15,
20,
25,
}

A comma after all elements, including the last element provides more usability when you modify code when you initialize array elements in a multiline statement.

By use of expression … you can also declare and initialize arrays, without specifying the length.

test := [...] { 5,10,15,20,25}
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)
}
Output:
C:\golang>go run arrays.go
Values of Array X:  [10 30 50 40 20]
Values of Array Y:  [100 200 0 500 0]
Values of Array Country:  [US UK Australia Russia Brazil]
Values of Array Transport:  [Train Bus Plane Car Bike]