Objective: Print array elements with formattingCreate a program that formats and prints array data.Approach:
Loop through an array and print each element with custom formatting.
Currency Converter
Objective: Use arrays for currency conversionStore exchange rates in an array and perform conversions.Key Concepts:
Parallel arrays
Array-based lookups
Calculations with arrays
Approach:
Create array of currency codes
Create array of exchange rates
Find index and calculate conversion
Hipster's Bookstore Search
Objective: Search through book dataBuild a search engine using arrays for book information.Approach:
Store book titles in an array
Search for matching titles
Display results
Find the Average
Objective: Calculate array statisticsCompute the average of numbers in an array.Approach:
sum := 0for _, num := range numbers { sum += num}average := float64(sum) / float64(len(numbers))
Number Sorter
Objective: Sort array elementsImplement a sorting algorithm for arrays.Approach:
Bubble sort
Selection sort
Or use the sort package
Word Finder
Objective: Search for words in an arrayFind specific words in an array of strings.Approach:
Loop through array and compare strings.
Objective: Convert array declarations to slicesRefactor array code to use slices instead.Approach:
Remove the size specification from array declarations.
Fix Slice Problems
Objective: Debug slice-related errorsFix common mistakes when working with slices.Common Issues:
Nil pointer dereferences
Index out of range
Type mismatches
Compare Slices
Objective: Learn slice comparison limitationsUnderstand why slices can’t be compared with ==.Key Concepts:
Objective: Dynamic slicing with user inputUse command-line arguments to control slicing.Approach:
Parse start and end indices from arguments and slice accordingly.
Slicing Housing Prices
Objective: Extract price rangesSlice housing price data to analyze specific ranges.Approach:
Use slicing to get recent prices, price ranges, etc.
Approach:
Use copy() or full slice expression to avoid sharing.
Sort Backing Array
Objective: Observe backing array effectsSee how sorting affects related slices.Approach:
Create multiple slices from one array, sort one, observe others.
Memory Allocations
Objective: Monitor slice memory behaviorObserve when new backing arrays are allocated.Approach:
Print capacity and address of backing array during appends.
Length and Capacity
Objective: Understand len vs capObserve how length and capacity change.Approach:
Objective: Master copy, append, and slicingWarm up with various slice manipulation exercises.Operations:
Copying slices
Inserting elements
Deleting elements
Concatenating slices
Limit Backing Array Sharing
Objective: Control slice exposurePrevent external code from modifying your slice’s backing array.Approach:
Use full slice expression [start:end:max] to limit capacity.
Fix Memory Leak
Objective: Prevent backing array retentionFix memory leaks caused by retaining large backing arrays.Approach:
Use copy() to create independent slices when keeping small portions of large slices.
Add Newlines
Objective: Complex slice manipulationInsert newlines after sentences using copy().Approach:
Create a new buffer and copy with insertions.
Print Daily Requests
Objective: Work with multi-dimensional slicesGroup and display data in a 2D slice structure.Approach:
requests := make([][]int, days)for i := range requests { requests[i] = make([]int, 24)}