Arrays in Go

Arrays are used to store multiple sequenced values in one variable.

var arrayName [arrayLength]dataType
//arrays are initialized with 0 values
arrayName[index] = value

//to initialize in one line
var initArrayName [arrayLength]dataType {element1, element2, ...}


Arrays are zero-indexed, which means the first element is 0 and the last element is the array length - 1.

The length of an array cannot be changed. For dynamic sizing, look at slices.

The length or size of an array can be obtained using the len(arrayName) function.

var array [2]int; //declaring an array of size 2 
array[0] = 1; //storing a value of 1 in the first element
array[1] = 2; //storing a value of 2 in the second element

< Referencing   |   Slices >

© 2018 SyntaxDB. All Rights Reserved.