04-The Anatomy of Arrays in Go. An Array Is A Container That Holds - by Uday Hiwarale - RunGo - Medium
04-The Anatomy of Arrays in Go. An Array Is A Container That Holds - by Uday Hiwarale - RunGo - Medium
Published in RunGo
You have 2 free member-only stories left this month. Upgrade for unlimited access.
Save
GOLANG
(source: pexels.com)
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 1/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
☛ What is an array
An array is a collection of the same data type. For example, an array of integers or an
array of strings. Since Go is a statically typed language, mixing different values
belonging to different data types in an array is not allowed.
In Go, an array has a fixed length. Once defined with a particular size, the size of an
array cannot be increased or decreased. But that problem can be solved using slices
which we will learn in the next lesson.
Hence to define a variable which is an array of 3 elements of the type int , it has the
following syntax.
package main
import "fmt"
func main() {
var a [3]int
fmt.Println(a)
}
In the above program, a is an array of 3 integer elements but we haven’t assigned any
values to individual array elements. What is your guess of Println statement. What is
the value of an empty array?
Since we haven’t assigned any value to a, we just defined the array but not the value of
array elements. So it will have zero value of its data type. For int , its zero value is 0
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 2/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
[0 0 0]
☛ Value assignment
We can individually assign values to each element of an array using their position in
the array AKA index number. In Go, the array index starts from 0 which is the first
element, hence last element index will be n-1 where n is the length of the array.
To access any element in the array, we need to use a[index] syntax where a is an
array variable. So let’s take our earlier example and modify it a little.
https://play.golang.org/p/_4DMJ3iRqRF
In the above program, we assigned new values to all 3 elements in the array using
a[index] syntax.
☛ Initial value
It would be pretty difficult to assign value to each element of an array if the array is
big. Hence Go provides short-hand syntax to define an array with an initial value or
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 3/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
array elements with pre-defined values. The syntax to define an array with initial
values is as following
You can also drop the data type declaration from the left-hand statement and Go will
infer the type from the array definition.
var a = [3]int{1, 2, 3}
a := [3]int{1, 2, 3}
There is absolutely no need to define all elements of an array. In the above example,
we could have defined the first two elements, leaving the third element a zero-value.
Search Medium
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 4/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/J4x7X8Vg73G
1.5K 6
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 5/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/IcwpC-fQQBj
Look carefully, we have used comma ( , ) at the end of the last element of the array.
This comma is necessary as if it wouldn’t be there, Go would have added a semicolon
( ; ) by the rule which would have crashed the program.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 6/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/UKl32kgngnF
The above program will print the same result because Go compiler guesses the value
of 4 from the number of elements of the array which are 4.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 7/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/wV9edsKhuh4
☛ Array comparison
As we discussed earlier in array definition, the array is a type in itself. [3]int is
different from [4]int which is very different from [4]string . It’s like comparing int
== string or apple == orange which is invalid and nonsense. Hence these arrays
cannot be compared with each other, unlike other programming languages.
While [3]int can be compared with [3]int even if their array elements do not match,
because they have the same data type.
For an array to be the equal or the same as the second array, both array should be of
the same type, must have the same elements in them and all elements must be in the
same order. In that case, == comparison will be true . If one or more of these
conditions do not match, it will return false .
Go matches first the data type and then secondly each element of the array with an
element of another array by the index.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 8/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/U933frU9Gql
So a, b, c and d, all of them have the same data type of [3]int . In the first
comparison, a == b , since a and b both contain the same element but different
order, this condition will be false . In the second comparison a == c , since c
But in the case of the third comparison, since both a and d contains the same
elements with the same order, a == d condition will be true . Hence above program
prints below result.
☛ Array iteration
To iterate over an array, we can use for loop.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 9/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/v5oGN2qQbgN
In the above example, since the element index in the array is always less than len(a) ,
But I absolutely hate the above way of iterating an array where I need to use index to
get the element of an array. But no worries, Go provides range operator which returns
index and value of each element of an array in for loop.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 10/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/XY9pONQaYng
range operator returns index and value of the element associated with an element
until all elements in the array are finished. If you are not interested in the index, we
can just assign it to the blank identifier.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 11/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/z0_v0CKquXU
☛ Multi-dimensional arrays
When array elements of an array are arrays, then it’s called a multi-dimensional array.
As from the definition of the array, an array is a collection of same data types and
array is a type in itself, a multi-dimensional array must have arrays that belong to the
same data type.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 12/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/7Hfeb30HD-H
Since we can use ... operator to guess the size of an array, Above program, can also
be written as
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 13/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
https://play.golang.org/p/w7oyAb_Acik
https://play.golang.org/p/_ZMipOrWy1R
In the above program, Go already knows the type of array elements which is [2]int
hence no need to mention it again. You could also use ... operator like
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 14/15
12/18/22, 2:33 PM The anatomy of Arrays in Go. An array is a container that holds… | by Uday Hiwarale | RunGo | Medium
Passed by value
When you pass an array to a function, they are passed by value like int or string
data type. The function receives only a copy of it. Hence, when you make changes to an
array inside a function, it won’t be reflected in the original array.
Why did I need to mention that? Because it’s not the case with slice which you will
see in upcoming tutorials.
https://medium.com/rungo/the-anatomy-of-arrays-in-go-24429e4491b7 15/15