Go Arrays, Slices & Maps – The Core Data Containers in Golang
Introduction – Why Learn Arrays, Slices & Maps in Go?
In Go, arrays, slices, and maps are the foundation of data structures. They allow you to organize data sequentially (arrays/slices) or in key-value pairs (maps). Go provides simple syntax, built-in safety, and efficient memory handling for these containers.
In this guide, you’ll learn:
- How to declare and manipulate fixed-size arrays
- How slices offer dynamic, resizable collections
- How maps store key-value pairs efficiently
- How to pass arrays/slices to functions
- How multidimensional arrays work in Go
Topics Covered
| Topic | Description |
|---|---|
| Go Arrays | Fixed-size collections of same-type elements |
| Go Multidimensional Arrays | Arrays of arrays—used for matrices or grids |
| Go Passing Arrays to Functions | Learn how arrays behave when passed by value or reference |
| Go Slices | Dynamic, resizable views over arrays |
| Go Maps | Key-value pairs with fast lookup and insertion |
Go – Arrays
Declare and Access
var nums [3]int = [3]int{10, 20, 30}
fmt.Println(nums[1]) // 20
Or shorthand:
nums := [...]int{1, 2, 3}
Key Properties:
- Fixed length
- Elements initialized to zero values
- Length set at compile time
Go – Multidimensional Arrays
matrix := [2][3]int{
{1, 2, 3},
{4, 5, 6},
}
fmt.Println(matrix[1][2]) // 6
Used in:
- Grid-based games
- Matrix math
- Tabular data
Go – Passing Arrays to Functions
Arrays are passed by value in Go (a full copy):
func update(arr [3]int) {
arr[0] = 99
}
func main() {
a := [3]int{1, 2, 3}
update(a)
fmt.Println(a[0]) // 1 (unchanged)
}
Use slices or pointers for memory-efficient mutation.
Go – Slices
Declare and Use
nums := []int{10, 20, 30}
fmt.Println(nums[0]) // 10
Slice from an array
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]
fmt.Println(slice) // [2 3 4]
Built-in Functions
| Function | Description |
|---|---|
len() | Length of slice |
cap() | Capacity from starting index |
append() | Add elements dynamically |
copy() | Copy elements from one slice to another |
nums := []int{1, 2}
nums = append(nums, 3, 4) // [1 2 3 4]
Slices are passed by reference-like behavior—backed by arrays.
Go – Maps
Declare and Use
ages := map[string]int{
"Alice": 25,
"Bob": 30,
}
fmt.Println(ages["Alice"]) // 25
Add / Update / Delete
ages["Eve"] = 22 // Add
delete(ages, "Bob") // Delete
val, exists := ages["Tom"]
Maps return two values on access: value and bool indicating existence.
Summary – Recap & Next Steps
Go’s array, slice, and map types provide lightweight yet powerful data containers. Slices and maps are used most commonly in real-world Go development thanks to their dynamic and flexible nature.
Key Takeaways:
- Arrays are fixed-size, zero-initialized collections
- Slices are dynamic, built on arrays, and allow resizing
- Maps store key-value pairs with fast access
- Arrays are passed by value; slices behave more like references
- Use
append,copy, anddeletefor data manipulation
Real-World Use Cases:
- Slices for dynamic lists (e.g., user inputs, query results)
- Maps for fast lookups (e.g., cache, dictionary)
- Arrays for performance-critical static datasets
Frequently Asked Questions
What’s the default value of an array in Go?
All elements are initialized to their zero value (e.g., 0 for int, "" for string).
Can I resize an array in Go?
No. Arrays are fixed size. Use slices for resizing.
Do slices copy the underlying array?
No. Slices share the underlying array, so changes reflect across copies unless explicitly cloned.
Are maps ordered in Go?
No. Go maps are unordered. Iteration order is not guaranteed.
How do I check if a map key exists?
Use:
val, ok := myMap["key"]
if ok {
fmt.Println("Exists:", val)
}
Share Now :
