<\/span><\/h2>\n\n\n\nAn array is a contiguous collection of elements of the same type. It is an ordered sequence of elements stored contiguously in memory<\/p>\n\n\n\n
Here is the format for the declaration of an array<\/p>\n\n\n\n
sample := [size_of_array]{type}{a1, a2... an}<\/code><\/pre>\n\n\n\n- size_of_array – number of elements in the array<\/li><\/ul>\n\n\n\n
- <type> is type of each element in the array<\/li><\/ul>\n\n\n\n
- a1, a2 … an are the actual elements.<\/li><\/ul>\n\n\n\n
In golang, the size of the array is part of its type. So This means that two arrays that have a different number of elements are of two different types and one cannot be assigned to another. Below error will be raised in case we try to assign two arrays of different length<\/p>\n\n\n\n
cannot use sample1 (type [1]int) as type [2]int in assignment<\/code><\/pre>\n\n\n\nThe code is:<\/p>\n\n\n\n
sample1 := [1]int{1}\nsample2 := [2]int{1,2}\n\nsample2 = sample1<\/code><\/pre>\n\n\n\nFor the same reason the length of array is fixed during create and cannot be changed later.<\/p>\n\n\n\n
<\/span>Declaration of an array<\/strong><\/span><\/h1>\n\n\n\nBoth number of elements and actual elements are optional in the array declaration.<\/p>\n\n\n\n
In below example, we see 4 ways of declaring of an array<\/p>\n\n\n\n
- Specifying both the length of the array and actual elements. Eg.<\/li><\/ul>\n\n\n\n
[2]int{1, 2}<\/code><\/pre>\n\n\n\n- Only length – In this case all the actual elements are filled up with default value zero of that type. Eg<\/li><\/ul>\n\n\n\n
[2]int{}<\/code><\/pre>\n\n\n\n- Only actual elements – In this case, the length of array will be equal to the number of actual elements. The symbol ‘…’ <\/strong>needs to be used within square brackets like this […]<\/strong> when not specifying the length. The symbol is an instruction to the compiler to calculate the length.<\/li><\/ul>\n\n\n\n
[...]int{2, 3}<\/code><\/pre>\n\n\n\n- Without length and actual elements – an empty array will be created in this case. Similar to above the symbol ‘…’ <\/strong>also needs to be used in this case as well.<\/li><\/ul>\n\n\n\n
[...]int{}<\/code><\/pre>\n\n\n\nLet’s see a code example illustrating above points. Also please keep in mind that the builtin function len() <\/strong>can be used to calculate the length of an array. In below program we are using len()<\/strong> function to calculate the length of the array.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Both number of elements and actual elements\n sample1 := [2]int{1, 2}\n fmt.Printf(\"Sample1: Len: %d, %v\\n\", len(sample1), sample1)\n\n \/\/Only actual elements\n sample2 := [...]int{2, 3}\n fmt.Printf(\"Sample2: Len: %d, %v\\n\", len(sample2), sample2)\n\n \/\/Only number of elements\n sample3 := [2]int{}\n fmt.Printf(\"Sample3: Len: %d, %v\\n\", len(sample3), sample3)\n\n \/\/Without both number of elements and actual elements\n sample4 := [...]int{}\n fmt.Printf(\"Sample4: Len: %d, %v\\n\", len(sample4), sample4)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nSample1: Len: 2, [1 2]\nSample2: Len: 2, [2 3]\nSample3: Len: 2, [0 0]\nSample4: Len: 0, []<\/code><\/pre>\n\n\n\nNotice in the above example that for sample3 <\/strong>variable the actual elements are filled up with the default value of int which is 0.<\/p>\n\n\n\nIt is also ok if the actual elements specified are less than the length of the array. The rest of the elements are filled up with the default value of the type specified. See the below example. The length of the array specified is 4 while only 2 actual elements are declared. Hence the remaining two elements are assigned value 0 which is the default zero value of an int<\/strong>.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n sample := [4]int{5, 8}\n fmt.Printf(\"Sample: Len: %d, %v\\n\", len(sample), sample)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nSample: Len: 4, [5 8 0 0]<\/code><\/pre>\n\n\n\n<\/span>Accessing array elements<\/strong><\/span><\/h1>\n\n\n\nSince array element are stored in contiguous manner, we can access an array element using an index. Similarly individual array elements can also be assigned a value using index. Accessing out of bound index will cause a compilation error. See below examples illustrating these points. The first index position will be zero<\/strong> and last will (length_of_array-1)<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n sample := [2]string{\"aa\", \"bb\"}\n\n fmt.Println(sample[0])\n fmt.Println(sample[1])\n\n sample[0] = \"xx\"\n fmt.Println(sample)\n \/\/sample[3] = \"yy\"\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\naa\nbb\n[xx bb]<\/code><\/pre>\n\n\n\nOn uncommenting the below line<\/p>\n\n\n\n
sample[3] = \"yy\"<\/code><\/pre>\n\n\n\n, it will give compilation error<\/p>\n\n\n\n
invalid array index 3 (out of bounds for 2-element array)<\/code><\/pre>\n\n\n\n