You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Go provides three essential data structures beyond basic types: slices (dynamic arrays), maps (hash tables), and pointers (memory addresses). Understanding these is crucial for writing effective Go code.
Arrays in Go have a fixed size determined at compile time:
var a [5]int // [0, 0, 0, 0, 0]
b := [3]string{"a", "b", "c"} // [a, b, c]
c := [...]int{1, 2, 3, 4} // size inferred: [1, 2, 3, 4]
Arrays are rarely used directly in Go because their size is part of the type — [3]int and [5]int are different types. Use slices instead.
A slice is a dynamically-sized, flexible view into an underlying array. Slices are Go's most commonly used data structure.
// Slice literal
nums := []int{1, 2, 3, 4, 5}
// make(type, length, capacity)
s := make([]int, 5) // len=5, cap=5, all zeros
s := make([]int, 0, 10) // len=0, cap=10, empty but pre-allocated
// Nil slice
var s []int // nil, len=0, cap=0
A slice is a three-field struct internally:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.