Go Mastery: A Comprehensive Guide - Part 3: Data Structures (Arrays, Slices, Maps)
Go Mastery: A Comprehensive Guide - Part 3: Data Structures (Arrays, Slices, Maps)
Introduction
Efficiently managing data is at the core of any backend system. Go provides a powerful set of built-in data structures—Arrays, Slices, and Maps—to handle collections of data with performance and flexibility.
Arrays
An array has a fixed size. The type and length are part of its identity.
1
2
3
var a [5]int // Array of 5 integers
a[0] = 100
fmt.Println(a[0])
Slices
Slices are more powerful than arrays because they are dynamic. A slice is a descriptor of an array segment.
1
2
3
s := []int{1, 2, 3} // Slice literal
s = append(s, 4) // Appending items (automatically resizes)
fmt.Println(s[1:3]) // Slicing: [2, 3]
Maps
Maps are Go’s built-in associative data type (a hash table).
1
2
3
4
5
6
m := make(map[string]int)
m["alice"] = 25
m["bob"] = 30
delete(m, "alice") // Deleting a key
val, exists := m["bob"] // Checking for key existence
Conclusion
Understanding these data structures is essential for writing effective Go code. Slices are used everywhere in Go because of their flexibility, while Maps are essential for efficient data lookups. In the next part, we will explore advanced features: Structs and Interfaces.
Suggested Reading
This post is licensed under
CC BY 4.0
by the author.