Working with Arrays in Go Programming

Arrays in Go Programming


In this article, we’ll discuss arrays implementation in Go programming. So, the array is where you take a bunch of a fixed-length series of elements of a chosen type.

So, you can make an array of bytes, you can make an array of integers, an array of floats with fix length. That’s the main thing about arrays, they are fixed length. It will decide compile-time how big they’re going to be. and compiler tells, how much space in memory it needs to allocate for these.

When we have different array elements. Each one is some chosen unique type, it’s an integer array or something else, you’ve got a set of each array element would be an integer.

We access elements of the array using the subscript notation [ ], each element index by using subscript notation. then we put the index right in the middle of the square brackets.

Initializes to Array

The element indices are initialized to a zero value and actually, this is not always the case in other languages. in the C language, you’re making an array, that array is not initialized at all. Unless you actually write code to initialize it. When you create it, it’s not initialized.

In Go language, the array initializes to zero value. When we say zero value, It means the zero value of the type that the array composes of. So, if the value is an integer, then the zero value is zero. If it’s string, then zero value is the empty string. So, in this following example, we’re showing how you can declare an array.

   var  x [5]int

   x[0] = 2

So, in the above example, we declare var x, and then in brackets five int type. it has the brackets five value, tells you this suppose to be an array of five integers. So, that’s how you declare an array. That would be initialized to zeros values by default. Then next line we refer to a particular element x bracket zero.

Array Literal

An array literal is a pre-defined set of values that make up an array. So, you use this to initialize arrays if you want to. So, for instance, declare here,

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

we have declared a var x array of five integer values. So, this is the way of initializing the array to a set of values, to an array of five integers. So, the array of five integer values that’s inside the curly brackets. That’s actually called an array literal.

The length of the literal has to be the same as the length of the array. So, if you declare x to be five, you better have five literals inside the array literal.

Then this is the operation that three dots […], That’s actually a keyword. So, three dots are used to express the size of an array literal. You can do that because if you have an array literal and inside the curly brackets you have four elements in there, then it’s clear that you want this array literal to be size four. You don’t have to say explicitly four. So, you can use four dots in there, and it will infer the size of the array from the number of elements inside the array literal. here is the following example.

x := [...]int{1, 2, 3, 4}

Iterating through Arrays

The most basic programming operation that you do to arrays, is to iterate through the array. when you look through each element of the array and you do something with the element of the array. but it is especially common in programming to iterate through an array and check out each element in the array.

you can see the below example, with sample array.

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

Actually, if you want to iterate through this array. then it will print out each element of the array.

for i, v range x 
   fmt.Printf("index %d, value %d",i,v)

	index  value
	0		1
	1		2
	2		3	
	3		4
	4		5

The right of the range keyword is the name of the array that we want to iterate through. So, range x. We have two variables here, i and v.

So, every pass of the loop, index, and value have bound to different values. i is the index of the element and v is the value.

Leave a Reply

Your email address will not be published. Required fields are marked *