0
0
Goprogramming~15 mins

Common slice operations in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Common slice operations
📖 Scenario: You are managing a list of daily temperatures recorded in a city. You want to practice common slice operations in Go to handle this data efficiently.
🎯 Goal: Build a Go program that creates a slice of temperatures, extracts a subset of days, appends new temperature data, and finally prints the updated slice.
📋 What You'll Learn
Create a slice of integers named temps with exact values: 23, 25, 22, 20, 19
Create a variable subset that slices temps from index 1 to 4 (exclusive)
Append the values 21 and 24 to the temps slice
Print the final temps slice
💡 Why This Matters
🌍 Real World
Handling lists of data like temperatures, sales, or scores is common in many programs. Knowing how to slice and append helps manage this data easily.
💼 Career
Many software jobs require working with lists or arrays. Mastering slice operations in Go is essential for backend development, data processing, and more.
Progress0 / 4 steps
1
Create the initial slice of temperatures
Create a slice of integers called temps with these exact values: 23, 25, 22, 20, 19
Go
Hint

Use temps := []int{23, 25, 22, 20, 19} to create the slice.

2
Slice the temperatures to get a subset
Create a variable called subset that slices the temps slice from index 1 to 4 (exclusive)
Go
Hint

Use subset := temps[1:4] to get the slice from index 1 to 3.

3
Append new temperature values
Append the values 21 and 24 to the temps slice using the append function
Go
Hint

Use temps = append(temps, 21, 24) to add new values.

4
Print the final temperatures slice
Print the temps slice using fmt.Println to display the updated list of temperatures
Go
Hint

Use fmt.Println(temps) to print the slice.