What is a common use case for Go in web development?
Go is often used to build fast and efficient web servers and APIs due to its strong support for concurrency and simple syntax.
Click to reveal answer
beginner
How does Go handle concurrent tasks in practical applications?
Go uses goroutines, which are lightweight threads managed by the Go runtime, allowing easy and efficient concurrent execution.
Click to reveal answer
beginner
Why is Go popular for building command-line tools?
Go compiles to a single binary with no dependencies, making it easy to distribute and run command-line tools across different systems.
Click to reveal answer
intermediate
What makes Go suitable for cloud services and microservices?
Go's fast startup time, efficient memory use, and built-in support for networking make it ideal for scalable cloud services and microservices.
Click to reveal answer
intermediate
How can Go be used in data processing tasks?
Go's concurrency features allow it to process large data sets efficiently by running multiple tasks in parallel, speeding up data processing.
Click to reveal answer
Which Go feature allows running multiple tasks at the same time?
AInterfaces
BPointers
CStructs
DGoroutines
✗ Incorrect
Goroutines are lightweight threads in Go that enable concurrent execution.
Why is Go a good choice for building command-line tools?
AIt requires a virtual machine to run
BIt compiles to a single binary with no external dependencies
CIt uses dynamic typing
DIt has a large standard GUI library
✗ Incorrect
Go compiles programs into a single executable file that can run independently.
What makes Go suitable for cloud microservices?
ASlow compilation
BHeavy runtime dependencies
CFast startup and efficient memory use
DLack of networking support
✗ Incorrect
Go's fast startup and efficient memory use help build scalable cloud microservices.
In Go, what is the main benefit of using goroutines for data processing?
AThey allow parallel processing to speed up tasks
BThey increase program size
CThey slow down execution
DThey prevent concurrency
✗ Incorrect
Goroutines enable running multiple tasks at once, making data processing faster.
Which of these is NOT a typical practical use case for Go?
AWriting mobile apps with native UI
BBuilding web servers
CCreating command-line tools
DDeveloping cloud microservices
✗ Incorrect
Go is not commonly used for native mobile UI apps; other languages are preferred for that.
Describe three practical use cases where Go is a strong choice and explain why.
Think about Go's speed, concurrency, and compilation features.
You got /5 concepts.
Explain how Go's concurrency model benefits real-world applications.
Focus on how goroutines make running many tasks easier and faster.
You got /5 concepts.
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