0
0
Goprogramming~5 mins

Basic input concepts in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afmt.Scanln
Bfmt.Println
Cfmt.Printf
Dfmt.Scanf
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
Which package must be imported to use fmt.Scanln?
Aos
Bio
Cfmt
Dbufio
If you want to read multiple values separated by spaces, which function is suitable?
Afmt.Scanln
Bfmt.Sprintf
Cfmt.Println
Dfmt.Scan
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
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.