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?
Afmt.Scanln
Bfmt.Println
Cfmt.Printf
Dfmt.Scanf
✗ Incorrect
fmt.Scanln reads input until a newline (Enter) is pressed.
What does the & symbol do when used in fmt.Scanln(&variable)?
APrints the variable
BPasses the variable's address to store input
CDeclares a new variable
DConverts variable to string
✗ 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?
Aos
Bio
Cfmt
Dbufio
✗ Incorrect
The fmt package provides Scanln for input.
If you want to read multiple values separated by spaces, which function is suitable?
Afmt.Scanln
Bfmt.Sprintf
Cfmt.Println
Dfmt.Scan
✗ Incorrect
fmt.Scan reads multiple space-separated inputs.
What happens if input type does not match the variable type in fmt.Scanln?
AAn error is returned and variable may not be set
BInput is automatically converted
CProgram crashes immediately
DInput is ignored silently
✗ 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.
Practice
(1/5)
1. What is the main purpose of using input functions in Go programs?
easy
A. To get information from the user during program execution
B. To print messages on the screen
C. To declare variables
D. To create new functions
Solution
Step 1: Understand input function role
Input functions allow the program to receive data from the user while it runs.
Step 2: Differentiate input from output
Printing messages is output, not input. Declaring variables or creating functions are unrelated.
Final Answer:
To get information from the user during program execution -> Option A
Quick Check:
Input = get user data [OK]
Hint: Input means getting data from user, not showing it [OK]
Common Mistakes:
Confusing input with output (printing)
Thinking input declares variables
Mixing input with function creation
2. Which of the following is the correct way to read a single word input from the user in Go?
easy
A. fmt.Println(variable)
B. fmt.Scanln(&variable)
C. bufio.NewReader(os.Stdin).ReadString()
D. var input string
Solution
Step 1: Identify input reading functions
fmt.Scanln reads a single word or token from user input into a variable.
Step 2: Eliminate other options
fmt.Println prints output, bufio.NewReader reads full lines, and declaring a variable alone doesn't read input.
Final Answer:
fmt.Scanln(&variable) -> Option B
Quick Check:
Single word input uses fmt.Scanln [OK]
Hint: Use fmt.Scanln for simple word input [OK]
Common Mistakes:
Using fmt.Println to read input
Confusing bufio.NewReader with Scanln for single words
Forgetting to pass variable address with &
3. What will be the output of this Go code snippet?
var name string
fmt.Print("Enter name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
If the user types: John and presses Enter.
medium
A. Hello,
B. Error: missing input
C. Enter name: John
D. Hello, John
Solution
Step 1: Understand input reading
fmt.Scanln reads the word 'John' into variable name.
Step 2: Understand output printing
fmt.Println prints "Hello," followed by the value of name, which is "John".
Final Answer:
Hello, John -> Option D
Quick Check:
Input 'John' prints 'Hello, John' [OK]
Hint: Scanln reads input; println shows it back [OK]
Common Mistakes:
Expecting input prompt to print again
Thinking Scanln reads empty string
Confusing output with input prompt
4. What is wrong with this Go code for reading a full line input?