📖 Scenario: Imagine you have a list of daily temperatures stored in an array. You want to look at a smaller part of this list, like the temperatures for just a few days, without copying the data. This is where slices come in handy in Go.
🎯 Goal: You will create an array of temperatures, then make a slice from it to see how slices and arrays relate. Finally, you will print the slice to see the selected temperatures.
📋 What You'll Learn
Create an array called temps with these exact values: 20, 22, 19, 24, 25, 23, 21
Create a slice called weekendTemps that includes the last three days from the temps array
Print the weekendTemps slice to show the temperatures for the weekend
💡 Why This Matters
🌍 Real World
Slices let you work with parts of data without copying everything, saving memory and time. This is useful in programs that handle large data like sensor readings or logs.
💼 Career
Understanding slices and arrays is essential for Go developers, especially when optimizing performance and memory usage in real-world applications.
Progress0 / 4 steps
1
Create the array of temperatures
Create an array called temps with these exact integer values: 20, 22, 19, 24, 25, 23, 21.
Go
Hint
Use [7]int{...} to create an array with 7 integers.
2
Create a slice from the array
Create a slice called weekendTemps that includes the last three elements from the temps array using slicing syntax.
Go
Hint
Use temps[4:7] to get elements from index 4 up to but not including 7.
3
Print the slice of weekend temperatures
Use fmt.Println to print the weekendTemps slice. Remember to import the fmt package at the top.
Go
Hint
Use fmt.Println(weekendTemps) to print the slice.
4
Modify the slice and observe the array
Change the first element of weekendTemps to 30. Then print both weekendTemps and the original temps array using fmt.Println to see how the array changes.
Go
Hint
Assign weekendTemps[0] = 30 and then print both variables.
Practice
(1/5)
1. In Go, what is the relationship between a slice and the underlying array it references?
easy
A. A slice is a completely independent copy of the array.
B. A slice is a view into an array and shares the same data.
C. A slice stores data separately and does not affect the array.
D. A slice can only reference arrays of fixed size 10.
Solution
Step 1: Understand slice basics
A slice in Go points to an underlying array and does not copy data.
Step 2: Relationship between slice and array
Changes to the slice affect the array and vice versa because they share the same data.
Final Answer:
A slice is a view into an array and shares the same data. -> Option B
Quick Check:
Slice shares array data = B [OK]
Hint: Remember: slices share data with arrays, not copy it [OK]
Common Mistakes:
Thinking slices copy array data
Believing slices are independent arrays
Assuming slices have fixed size
2. Which of the following is the correct way to create a slice from an array arr := [5]int{1,2,3,4,5} that includes elements 2, 3, and 4?
easy
A. slice := arr[1:4]
B. slice := arr[2:5]
C. slice := arr[0:3]
D. slice := arr[3:6]
Solution
Step 1: Identify slice indices
Slice syntax is arr[start:end], including start but excluding end index.
Step 2: Select correct indices for elements 2,3,4
Elements 2,3,4 are at indices 1,2,3, so slice is arr[1:4].
Final Answer:
slice := arr[1:4] -> Option A
Quick Check:
Slice indices 1 to 4 exclude 4 = D [OK]
Hint: Slice includes start index, excludes end index [OK]
Common Mistakes:
Using wrong start or end index
Trying to slice beyond array length
Confusing inclusive/exclusive slice bounds
3. What will be the output of the following Go code?
D. Index out of range error when assigning to slice[2]
Solution
Step 1: Check slice length
slice := arr[1:3] includes elements at indices 1 and 2, length 2.
Step 2: Accessing slice[2]
slice[2] tries to access third element (index 2), which is out of range.
Final Answer:
Index out of range error when assigning to slice[2] -> Option D
Quick Check:
Slice length 2, index 2 invalid = C [OK]
Hint: Slice length limits valid indices from 0 to len-1 [OK]
Common Mistakes:
Assuming slice length equals array length
Using invalid slice indices
Ignoring runtime panics
5. Given the array arr := [5]int{1, 2, 3, 4, 5}, you create two slices: slice1 := arr[1:4] slice2 := arr[2:5] If you set slice1[1] = 99, what will be the content of slice2?
hard
A. [99 4 5]
B. [3 99 5]
C. [3 4 5]
D. [2 99 5]
Solution
Step 1: Identify slice1 and slice2 elements
slice1 = arr[1:4] = elements at indices 1,2,3 -> [2,3,4] slice2 = arr[2:5] = elements at indices 2,3,4 -> [3,4,5]
Step 2: Modify slice1[1]
slice1[1] is element at arr index 2 (value 3), changed to 99. So arr becomes [1,2,99,4,5].