List / Array / Slices in Go
Guide to Go slices: creation, length, access, mutation, traversal, searching, joining, comparison, insertion, removal, reversing, cloning, flattening, deduplication, and using the slices package.
Slice Creation
Slices are the main dynamic list type in Go. Create them with composite literals, make(), or by slicing an array. Go also has fixed-size arrays, but slices are used most of the time.
1. Slice Literal
Create slices using composite literal syntax.
2. make() and Fixed-Size Arrays
make creates a slice with length and optional capacity. Arrays have fixed size.
Slice Fundamentals
Slices have a length (len) and optional capacity. Access and update by index. Removing an element requires building a new slice with append.
1. Slices Are Typed
All elements must be the same type.
2. Getting Length
Use len(s).
3. Accessing Element at Index
Use s[i]. Panic if index is out of range.
4. Updating Element at Index
Assign with s[i] = value.
5. Removing Element at Index
Append the two parts around the index.
Traversing a Slice
Use a classic for loop with index, or for range to get index and value (or value only).
1. for Loop with Index
Index-based traversal.
2. for range (Values)
Value-based traversal.
3. for range (Index and Value)
When you need both.
Searching, Joining & Comparing
Use loops or the slices package (Go 1.21+) for search. Join with strings.Join. Compare with reflect.DeepEqual or a loop.
1. Check if Element Exists
Loop or slices.Contains.
2. Find Element Index
Loop or slices.Index.
3. Joining Slice Elements (Strings)
Use strings.Join.
4. Comparing Slices by Contents
Use reflect.DeepEqual or slices.Equal.
Insertion & Removal
append adds to the end. "Pop" by reslicing. "Shift" and "unshift" by building a new slice with append.
1. Insert/Remove from End
append to add; reslice to pop.
2. Insert/Remove from Start
Prepend with append; shift by reslicing.
3. Insert & Remove at Specific Index
Insert with append; remove with append(s[:i], s[i+1:]...).
Advanced Slice Operations
Reverse and shuffle with loops or packages. Concatenate with append. Flatten and deduplicate with loops. Clone by appending to a new slice or using slices.Clone.
1. Reverse a Slice
Swap elements in a loop or use slices.Reverse.
2. Clear a Slice
Reslice to zero length or assign nil.
3. Shuffle a Slice
Use rand.Shuffle.
4. Multi-dimensional Slice
Slice of slices.
5. Concatenating Slices
Use append(s1, s2...).
6. Flattening Slices
Append each nested element in a loop.
7. Removing Duplicates
Use a map to track seen elements.
8. Shallow & Deep Clone
Shallow: append or slices.Clone. Deep: marshal to JSON then unmarshal.
Slice Operations (map, filter, sort, reduce)
Go has no built-in map/filter on slices. Use for range loops or the helpers from the Functions chapter. Sorting is in sort package.
1. Transform (map-style)
Loop and build new slice.
2. Filter
Loop and append when condition holds.
3. Sort
Use sort package.
4. Reduce (aggregate)
Loop with an accumulator.