GoLang 101 — (13) The Array Data Type of GoLang

Handhika Yanuar Pratama
4 min readApr 13, 2024
Photo by Chinmay B on Unsplash

Yeah, Day 14

In Go, an array is a fixed-size collection of elements of the same type. The size of an array is determined at compile time and cannot be changed at runtime. Arrays in Go are indexed starting from 0, meaning the first element has index 0, the second element has index 1, and so on. Here’s how you declare an array in Go.

// Declares an array 'a' of type 'int' with a length of 5
var a [5]int

In this example, a is an array of integers (int) with a length of 5. You can also initialize the array with values.

// Initializes an array 'b' of type 'string' with values
b := [3]string{“apple”, “banana”, “orange”}

In this case, b is an array of strings with a length of 3, and it’s initialized with the values “apple”, “banana”, and “orange”.

Key Points

Here’s some key points the usage of an array in Go.

  • Fixed Size: As mentioned earlier, the size of an array is fixed and determined at compile time. Once defined, you cannot change the size of the array.
  • Zero-based Indexing: Array elements are accessed using zero-based indexing. The first element is at index 0, the second element at index 1, and so on.
  • Homogeneous Elements: All elements in an array must be of the same type. You cannot mix different types within the same array.
  • Value Semantics: When you assign one array to another, a copy of the array is made. Changes to the elements of one array do not affect the elements of the other.
  • Passing Arrays to Functions: When you pass an array to a function, a copy of the array is passed unless you use pointers or slices. It will be explore later.

Languages like Python and JavaScript have data structures that behave similarly to arrays but are not fixed in size. In Python, the equivalent of an array is the list data structure, which can grow or shrink dynamically as elements are added or removed. Similarly, in JavaScript, arrays are dynamic and can be resized by adding or removing elements.

Examples

Here’s an example demonstrating the usage of an array in Go.

package main

import “fmt”

func main() {
var fruits [3]string
fruits[0] = "apple"
fruits[1] = "banana"
fruits[2] = "orange"
fmt.Println("Fruits:", fruits)

// Iterating over the array
for i := 0; i < len(fruits); i++ {
fmt.Println("Fruit:", fruits[i])
}
}

This program declares an array fruits of type string with a length of 3, initializes it with values, and then iterates over the array to print each element. Here are the results.

Pros and Cons of Fixed Arrays

Dynamic arrays in Python and JavaScript may offer more flexibility, but fixed-size arrays possess several advantages over their dynamic counterparts.

  • Predictable Memory Usage: Fixed-size arrays makes memory usage predictable and avoids the overhead of dynamic memory allocation and deallocation.
  • Faster Access: Fixed-size arrays use contiguous memory locations it makes accessing element become faster.
  • Compile-Time Safety: Fixed-size can detect errors related to array indices and sizes at compile time.
  • Simple Implementation: Fixed-size arrays do not require dynamic memory management operations such as resizing, reallocating, and copying elements.
  • Thread Safety: Fixed-size arrays have size that cannot change during runtime. This simplifies concurrent access to the array in multithreaded environments.
  • Memory Efficiency: In some cases, fixed-size arrays may be more memory efficient than dynamic arrays because they do not incur overhead from dynamic memory management structures such as heap metadata or extra capacity.

However, it’s essential to note that fixed-size arrays also have limitations, such as the inability to resize or grow dynamically, which may restrict their use in scenarios where the size of the data collection is not known in advance or may change during runtime.

Dynamic arrays, on the other hand, provide flexibility and can adapt to changing requirements but may incur performance overhead and memory fragmentation.

Conclusions

In summary, arrays in Go provide efficient storage and access for fixed-size collections of homogeneous elements. While they offer performance benefits and type safety, their inflexibility may require developers to consider alternative data structures like slices for scenarios requiring dynamic resizing or variable-sized collections.

--

--