This post is only a short article from me and consider it as documentation (from myself) when learning Go. I will not tell you a detail about the golang itself, so let’s look further at the fundamental array in golang.

Array

Let’s revisit a little concept about arrays. Array is one of the 4 basic data structures in golang besides slice, map, and struct. Array itself is an arrangement of objects or variables that can be represented as integers, strings or boolean. Arrays are arranged in order that is sorted by index and has the same data type. We can store many objects with a large number in the array and each element in the array that has been declared can not be changed again (removed or inserted), but we can set a value for that element based on any index.

In Golang, an array is a numbered sequence of elements of a specific length. Basically, there are two methods that we can define array:

  • We declare an array outside the scope of the function. This array is a global array, which can be accessed in other packages with a particular package name. For example:
package main
 
import "fmt"
 
// define array outside scope function
var groceryObject [4]string
 
func main() {
	groceryObject[0] = "apple"
	groceryObject[1] = "basket"
	groceryObject[2] = "juice"
	groceryObject[3] = "orange"
 
	fmt.Println("grocery store has object", groceryObject)
}
  • We also can declare arrays inside a function scope which results in the array being limited to that function, you only need to pass the groceryObject variable into the function scope and then, the results will remain the same as those above. Another way to go, we can also declare an array without representing the data type of the array by using a declarator like := which can be seen in the following code:
package main
 
import "fmt"
 
func main() {
	groceryStore := [3]int{3,5,6}
	fmt.Println("grocery store has number", groceryStore)
}

Another Array Examples

Another example of arrays, if we declare an array without setting the value, then the value will be zero, this is according to the documentation where by default an array is zero-valued. We can also set a value on an existing array by index, like this

// define array with 3 indexes
var scopeStore [3]int
 
// set a value for index 2
scopeStore[2] = 1

if we run the program above, the result is [0 0 0], since scopeStore has no previous value (meaning zero) and then we set the value for index 2 then the output value in the array changes to [0 0 1].

We can also use the built-in function in golang, to return a length of the array with len

groceryStore := [5]int{7,4,1,2,4}
	
fmt.Println("Length of array", len(groceryStore))

We can also initialize the values in an array in one line just like in a variable groceryStore. We can also initialize an array without setting the value of the array element, when we use the ... identifier and using the reflect package it allows a program to manipulate objects with arbitrary types and extract type information

import (
    "reflect"
)
groceryShop := [...]string{"apple", "orange", "juice"}
 
fmt.Println(reflect.ValueOf(groceryShop).Kind())
fmt.Println("Length of array", len(groceryShop))

To expand further, we can also check whether an element exists in an array or not. By checking a conditional to determine if the elements searched in the array have the same data type, then iterating through every element in an array

func main() {
	groceryShop := [...]string{"apple", "orange", "juice"}
	fmt.Println(isExist(groceryShop, "apple"))
}
 
// function for checking the element of array
func isExist(arrayType interface{}, item interface{}) bool {
	arr := reflect.ValueOf(arrayType)
 
	if arr.Kind() != reflect.Array {
		panic("Invalid")
	}
 
	for i := 0; i < arr.Len(); i++ {
		if arr.Index(i).Interface() == item {
			return true
		}
	}
	return false
}

Additional Notes

This is just the basics of how arrays in Golang can work, from this post we can conclude:

  • Once you declare an array with its element, you can’t change it.
  • If the value in the array is not set, it will be zero.
  • We can set or replace values in an array based on an existing index using array[index] = value
  • We can also return length of the array using len function
  • For array comparison, will return true if both arrays have the same data type and same elements, otherwise it will return false
  • Last but not least, since array is one-dimensional, you can compose it into a multi-dimensional type with iteration