Go – Arrays, Slices & Maps
Estimated reading: 3 minutes 43 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 :

Leave a Reply

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

Share

Go – Arrays

Or Copy Link

CONTENTS
Scroll to Top