Skip to content

Arrays, Slices & Maps

In JavaScript/TypeScript, arrays are dynamic by default and objects serve as maps. Go separates these concerns: fixed-size arrays, dynamic slices (the one you’ll use daily), and maps.

Go arrays have a fixed size baked into the type. You’ll rarely use them directly. Slices are the dynamic, flexible version — backed by an array but with a length and capacity that grow as needed.

TypeScript
// TypeScript — always dynamic
const nums: number[] = [1, 2, 3];
nums.push(4);
console.log(nums.length); // 4
Go
// Go — slice (dynamic)
nums := []int{1, 2, 3}
nums = append(nums, 4)
fmt.Println(len(nums)) // 4
TypeScript
const letters = ["a", "b", "c", "d"];
// slice (non-mutating)
letters.slice(1, 3) // ["b", "c"]
// length
letters.length // 4
Go
letters := []string{"a", "b", "c", "d"}
// slice expression
letters[1:3] // ["b", "c"]
// length
len(letters) // 4

Go maps are like TypeScript Record<K,V> or Map<K,V>. Keys can be any comparable type.

TypeScript
// TypeScript
const scores: Record<string, number> = {
alice: 95,
bob: 87,
};
scores["carol"] = 91;
delete scores["bob"];
console.log(scores["alice"]);
Go
// Go
scores := map[string]int{
"alice": 95,
"bob": 87,
}
scores["carol"] = 91
delete(scores, "bob")
fmt.Println(scores["alice"])
package main
import (
"fmt"
"sort"
)
func main() {
// Slice
fruits := []string{"banana", "apple", "cherry"}
fruits = append(fruits, "date")
sort.Strings(fruits)
fmt.Println("Fruits:", fruits)
// Map
count := map[string]int{}
words := []string{"go", "is", "fun", "go", "is", "great", "go"}
for _, w := range words {
count[w]++
}
fmt.Println("Word counts:", count)
}
Which Go collection type grows dynamically (like a JS array)?
How do you append to a slice in Go?
What does the two-value map lookup (val, ok := m[key]) tell you?
What is the zero value returned when accessing a missing int map key?