Hi folks! In this blog we are gonna have A Basic Understanding of Array in GoLang.Array are the probably one of the most used data structure throughout programming.They are quite useful and it is versatile.It hold a bunch of data.
Array
In Go and most programming languages arrays are fixed length of sequence of elements and all the items in the array have to be of the same type.An array holds a specific number of elements, and it cannot grow or shrink.So you want to store some numbers the array has to be all of the same type of numbers like integers or floats or whatever.Array in some programming language technically is just a pointer to some location in memory and that location is known as contiguous part of memory that contains all the elements that kind of makes up the array.
Declaration of Array in GoLang
Now we will see how we can declare an array and how to use array.To declare an array we use keyword var then we will have a name array and then the type of array like String,Integers,etc.
There are different ways to declare arrays. Let’s look at them one by one.
package main
import "fmt"
func main() {
var arr [4]int
fmt.Println(arr)
}
The Number in [ ] tells the compiler that we are gonna have array of three integers.In go when you declare a variable in automatically initialize to zero value.If we print our array we should a bunch of zeros.The output of the above code will print.
[0 0 0 0]
Also if you want to get element of a particular index you can do that like below.It will give the value of current index.Here the value of that index is zero so it will print zero.
fmt.Println(arr[0])
If we pass 4 in the index value then it will return invalid array index 4 (out of bounds for 4-element array). Because the index value start with 0.
fmt.Println(arr[4])
Also if we want the length of an array we can use len()
function.
fmt.Println(len(arr))
We can also declare and initialize the value at the same time so we will make an array.
package main
import "fmt"
func main() {
var arr2 [4]int = [4]int {1, 2, 3, 4}
fmt.Println(arr2)
}
Another way we can declare an array like below.
package main
import "fmt"
func main() {
arr3 := [4]int {1, 2, 3, 4}
fmt.Println(arr3)
}
The output of the both above array will return.
[1 2 3 4]
NOTE: arr2 = arr3 NOT compatible type.
And finally there is kind of like a shorthand to create an array we can use the brackets [ …] with ellipsis and let the compiler find the length for you. This is done in the following program.
package main
import "fmt"
func main() {
arr4 := [...]int {5: -1}
fmt.Println(arr4)
fmt.Println(len(arr4))
}
In the above program I have declared an array and I did not have specified the length of the array also I have initialize the value of 5th index position.We can also do that thing in array.The output of the above code will return.
[0 0 0 0 0 -1]
6
Multidimensional Array
The arrays we created so far are all single dimension. It is possible to create multidimensional arrays in go lang.For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array −
var twodim [4][6]int
package main
import "fmt"
func main() {
var a = [4][2]int{ {0,0}, {1,2}, {2,4}, {3,6}}
var i, j int
for i = 0; i < 4; i++ {
for j = 0; j < 2; j++ {
fmt.Println("a[%d][%d] = %d", i,j, a[i][j] )
}
}
}
The above program will return the output.
a[0][0] = 0
a[0][1] = 1
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 3
a[3][0] = 3
a[3][1] = 4
That’s all about array in this blog if you want to learn Slices in go then refer to below link:
https://blog.knoldus.com/a-basic-introduction-about-slice-in-golang/
Reference:
