Go – Arrays, Slices & Maps
Estimated reading: 3 minutes 373 views

Go – Arrays Explained with Syntax, Examples, and Use Cases (2025 Guide)

Introduction – What Are Arrays in Go?

In Go, an array is a fixed-size collection of elements of the same type. Unlike slices, arrays are not resizable, making them ideal for static data structures, fixed buffers, and low-level memory-safe operations. Each array has a known size and indexed elements starting from 0.

In this section, you’ll learn:

  • How to declare, initialize, and access arrays
  • Array indexing and iteration techniques
  • Difference between arrays and slices
  • Common use cases and limitations

Basic Syntax – Declare and Initialize Arrays

var a [3]int
a[0] = 10
a[1] = 20
a[2] = 30

fmt.Println(a)

Output:

[10 20 30]

a is an array of 3 integers with values assigned using indices.


Short Declaration and Initialization

b := [3]string{"Go", "Java", "Rust"}
fmt.Println(b)

Output:

[Go Java Rust]

You can also let Go infer the size:

c := [...]float64{3.14, 1.61, 2.71}
fmt.Println(len(c))  // Output: 3

Accessing Elements by Index

names := [3]string{"Alice", "Bob", "Charlie"}
fmt.Println(names[1]) // Output: Bob

Index starts at 0 and ends at length - 1.


Iterating Over Arrays – for Loop

arr := [4]int{10, 20, 30, 40}
for i := 0; i < len(arr); i++ {
    fmt.Println("Index", i, "=", arr[i])
}

Classic for loop with index access.


Iterating with range

arr := [3]int{5, 10, 15}
for i, v := range arr {
    fmt.Println("Index:", i, "Value:", v)
}

range provides both index and value.


Arrays vs Slices in Go

FeatureArraysSlices
SizeFixed at compile timeDynamic (resizable)
MemoryValue type (copied on assignment)Reference type (backed by array)
Use CaseBuffers, static-size collectionsDynamic lists, most Go programs
Syntax[N]Type{}[]Type{}

Arrays Are Value Types

When you assign an array to another variable, Go makes a copy, not a reference:

a := [3]int{1, 2, 3}
b := a
b[0] = 100
fmt.Println(a) // [1 2 3]
fmt.Println(b) // [100 2 3]

Use slices if you want reference-like behavior.


Best Practices

  • Use arrays for fixed-size collections (e.g., days of the week, RGB values)
  • Don’t use arrays for dynamic listsβ€”prefer slices
  • Use [...]Type{} for inferred-size declarations
  • Iterate with range for readability and performance

Summary – Recap & Next Steps

Arrays in Go are efficient and predictable, ideal for situations where size doesn’t change. While they’re less flexible than slices, arrays give you better control and memory safety.

Key Takeaways:

  • Arrays are fixed-length, same-type data containers
  • Use index-based or range-based access
  • Arrays are value typesβ€”assignments are copied
  • Prefer slices for flexible, real-world data lists

Next: Learn about Go Slices, which are dynamic views into arrays and more commonly used in modern Go code.


FAQs – Go Arrays

Can I change the size of an array in Go?
No. Arrays are fixed-size. Use slices for dynamic resizing.

Are arrays in Go value or reference types?
Arrays are value types. Assigning one array to another copies the data.

How do I get the length of an array in Go?
Use len(array).

What is the zero value of an array?
All elements are initialized to the zero value of their type (e.g., 0 for int, "" for string).

Can I loop through arrays with range?
Yes. range gives index and value on each iteration.


Share Now :
Share

Go – Arrays

Or Copy Link

CONTENTS
Scroll to Top