Recall & Review
beginner
What package is commonly used in Go to read input from the user?
The
fmt package is commonly used to read input from the user in Go, especially with functions like fmt.Scan and fmt.Scanln.Click to reveal answer
intermediate
How does
fmt.Scanln differ from fmt.Scan in Go?fmt.Scanln reads input until a newline is encountered, while fmt.Scan reads input separated by spaces and stops at a newline or space.Click to reveal answer
beginner
Write a simple Go code snippet to read a string input from the user.
```go
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
}
```Click to reveal answer
beginner
Why do we use the ampersand (&) before a variable when reading input in Go?
The ampersand (&) is used to pass the memory address of the variable to the input function, so the function can store the input value directly into that variable.
Click to reveal answer
intermediate
What happens if the user inputs a different data type than expected in Go's
fmt.Scanln?If the input type does not match the variable type,
fmt.Scanln will return an error and the variable may not be set correctly.Click to reveal answer
Which Go function reads input until the user presses Enter?
✗ Incorrect
fmt.Scanln reads input until a newline (Enter) is pressed.What does the & symbol do when used in
fmt.Scanln(&variable)?✗ Incorrect
The & symbol passes the memory address of the variable so input can be stored there.
Which package must be imported to use
fmt.Scanln?✗ Incorrect
The
fmt package provides Scanln for input.If you want to read multiple values separated by spaces, which function is suitable?
✗ Incorrect
fmt.Scan reads multiple space-separated inputs.What happens if input type does not match the variable type in
fmt.Scanln?✗ Incorrect
Mismatched types cause an error and the variable may not be assigned.
Explain how to read a string input from the user in Go using
fmt.Scanln.Think about how you ask a friend for their name and store it.
You got /4 concepts.
Describe why the ampersand (&) is necessary when reading input into a variable in Go.
Imagine giving someone your mailbox address so they can put a letter inside.
You got /4 concepts.