📖 Scenario: You are working on a small store inventory system. You want to keep track of products and their prices, then find which products are affordable under a certain budget.
🎯 Goal: Build a Go program that stores product prices, sets a budget limit, filters products affordable within that budget, and prints the affordable products.
📋 What You'll Learn
Create a map called products with product names as keys and prices as float64 values
Create a variable called budget with a float64 value
Use a for loop to find products with prices less than or equal to budget
Store affordable products in a new map called affordable
Print the affordable map
💡 Why This Matters
🌍 Real World
Managing product prices and budgets is common in retail and inventory systems.
💼 Career
Understanding maps, loops, and conditionals in Go is essential for backend and systems programming jobs.
Progress0 / 4 steps
1
Create the products map
Create a map called products with these exact entries: "Apple": 1.20, "Banana": 0.80, "Cherry": 2.50, "Date": 3.00, "Eggplant": 1.50
Go
Hint
Use a map literal with string keys and float64 values.
2
Set the budget variable
Create a variable called budget with the float64 value 2.00 inside the main function, below the products map.
Go
Hint
Use a simple variable declaration with := and a float64 value.
3
Filter affordable products
Create a new map called affordable of type map[string]float64. Use a for loop with variables product and price to iterate over products. Inside the loop, add products with price less than or equal to budget to the affordable map.
Go
Hint
Use make to create the map, then a for loop with range to iterate.
4
Print the affordable products
Use fmt.Println to print the affordable map. Remember to import the fmt package at the top.
Go
Hint
Use fmt.Println(affordable) to print the map.
Practice
(1/5)
1. Which Go feature is commonly used to create a simple web server that listens on a port?
easy
A. The net/http package with http.ListenAndServe
B. The fmt package with Println
C. The os package with Open
D. The math package with Pow
Solution
Step 1: Identify the package for web servers
The net/http package provides tools to create web servers in Go.
Step 2: Recognize the function to start the server
http.ListenAndServe starts a server listening on a specified port.
Final Answer:
The net/http package with http.ListenAndServe -> Option A
Quick Check:
Web server = net/http.ListenAndServe [OK]
Hint: Web servers use net/http.ListenAndServe to listen on ports [OK]
Common Mistakes:
Confusing printing functions with server functions
Using file operations instead of network functions
Thinking math package handles networking
2. Which of the following is the correct syntax to open a file named "data.txt" for reading in Go?
easy
A. file, err := os.Open("data.txt")
B. file := os.OpenFile("data.txt", os.O_WRONLY)
C. file, err := ioutil.ReadFile("data.txt")
D. file := os.Read("data.txt")
Solution
Step 1: Identify the function to open a file for reading
os.Open opens a file in read-only mode and returns a file and error.
Step 2: Check the syntax correctness
The syntax file, err := os.Open("data.txt") correctly assigns both file and error.
Final Answer:
file, err := os.Open("data.txt") -> Option A
Quick Check:
Open file for reading = os.Open [OK]
Hint: Use os.Open with two variables to open files safely [OK]
Common Mistakes:
Using os.Read which does not exist
Using ioutil.ReadFile which reads whole file, not opens
Opening file with wrong flags for reading
3. What will be the output of this Go code snippet?
package main
import "fmt"
func main() {
nums := []int{2, 4, 6}
squares := make(map[int]int)
for _, n := range nums {
squares[n] = n * n
}
fmt.Println(squares)
}
medium
A. map[2:4 4:8 6:12]
B. map[2:4 4:16 6:36]
C. map[2:8 4:16 6:36]
D. map[2:2 4:4 6:6]
Solution
Step 1: Understand the loop and map assignment
The loop goes through each number in nums and assigns squares[n] = n * n.
Step 2: Calculate each square value
2*2=4, 4*4=16, 6*6=36, so the map stores these pairs.
Final Answer:
map[2:4 4:16 6:36] -> Option B
Quick Check:
Squares map = map with keys and squared values [OK]
Hint: Map keys with squares store n:n*n pairs [OK]
Common Mistakes:
Confusing keys and values in map
Multiplying incorrectly
Expecting slice output instead of map
4. This Go code tries to read a file but has an error. What is the problem?
os.Open returns two values: file and error. The code only assigns one variable.
Step 2: Identify missing error handling
Without checking error, the program may panic if file does not open.
Final Answer:
Missing error check after os.Open -> Option C
Quick Check:
Always check error from os.Open [OK]
Hint: Always assign and check error from os.Open [OK]
Common Mistakes:
Ignoring error returned by os.Open
Assuming defer is wrong here
Thinking file path must be absolute always
5. You want to create a Go program that reads a list of numbers from a file, squares each number, and writes the results to a new file. Which approach best combines Go features for this task?
hard
A. Use fmt.Println to read and write files directly
B. Use net/http to read files and fmt.Scan to write
C. Use math.Pow to read files and os.Remove to write
D. Use os.Open to read, loop to square, and os.Create with WriteString to save
Solution
Step 1: Reading numbers from a file
os.Open opens the file for reading; then you can scan or read lines.
Step 2: Processing and writing results
Loop through numbers, square them, then use os.Create to create output file and WriteString or similar to write results.
Final Answer:
Use os.Open to read, loop to square, and os.Create with WriteString to save -> Option D