📖 Scenario: Imagine you want to create a simple program that asks a user for their name and then says hello to them. To do this, the program needs to get information from the user (input) and then show a message back (output).
🎯 Goal: Build a small Go program that asks for a user's name and then prints a greeting message using that name.
📋 What You'll Learn
Use fmt.Scanln to get input from the user
Use fmt.Println to show output to the user
Store the input in a variable called name
Print a greeting message that includes the name
💡 Why This Matters
🌍 Real World
Many programs need to ask users questions and show answers, like calculators, games, or websites.
💼 Career
Understanding input and output is a basic skill for any programmer to make interactive applications.
Progress0 / 4 steps
1
Create a variable to store the user's name
Declare a variable called name of type string to hold the user's input.
Go
Hint
Use var name string to create a variable that can hold text.
2
Get the user's name as input
Use fmt.Scanln(&name) to read the user's input and store it in the variable name.
Go
Hint
Use fmt.Scanln(&name) to read what the user types and save it in name.
3
Print a greeting message using the input
Use fmt.Println with a message that says Hello, followed by the value of name.
Go
Hint
Use fmt.Println("Hello,", name) to show the greeting.
4
Run the program and see the output
Run the program. When it asks for input, type GoLearner and press Enter. The program should print Hello, GoLearner.
Go
Hint
Type GoLearner when the program waits for input, then press Enter to see the greeting.
Practice
(1/5)
1. Why do programs need input and output?
easy
A. To avoid writing code
B. To run faster without errors
C. To use more memory
D. To interact with users and show results
Solution
Step 1: Understand the role of input
Input allows programs to receive information from users or files, which is necessary to perform tasks.
Step 2: Understand the role of output
Output lets programs display results or messages back to users, completing the interaction.
Final Answer:
To interact with users and show results -> Option D
Quick Check:
Input and output enable interaction = A [OK]
Hint: Input gets data; output shows results [OK]
Common Mistakes:
Thinking input/output only affects speed
Believing programs don't need to communicate
Confusing input/output with memory usage
2. Which Go code snippet correctly reads input from the user?
easy
A. fmt.Scanln(&name)
B. fmt.Println(name)
C. var name string = input()
D. read(name)
Solution
Step 1: Identify correct input function in Go
In Go, fmt.Scanln(&variable) reads input from the user into the variable.
Step 2: Check other options for correctness
fmt.Println prints output, not input; input() and read() are not valid Go functions.
Final Answer:
fmt.Scanln(&name) -> Option A
Quick Check:
Use fmt.Scanln for input = C [OK]
Hint: Use fmt.Scanln(&var) to read input [OK]
Common Mistakes:
Using fmt.Println to read input
Assuming input() exists in Go
Forgetting the & before variable
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter name: ")
fmt.Scanln(&name)
fmt.Println("Hello,", name)
}
medium
A. Syntax error
B. Enter name: Hello,
C. Hello,
D. Hello, name
Solution
Step 1: Understand input reading
The program asks for input with fmt.Print, then reads user input into variable 'name'.
Step 2: Understand output printing
It prints "Hello," followed by the input value stored in 'name'.
Final Answer:
Hello, <user input> -> Option C
Quick Check:
Input read and printed = D [OK]
Hint: Input stored in variable, then printed [OK]
Common Mistakes:
Expecting prompt and output on same line
Confusing variable name with string literal
Thinking code has syntax errors
4. Find the error in this Go code that reads input and prints it:
package main
import "fmt"
func main() {
var age int
fmt.Scanln(age)
fmt.Println("Age is", age)
}
medium
A. fmt.Println cannot print variables
B. Missing & before variable in fmt.Scanln
C. Variable age should be string
D. No import for fmt package
Solution
Step 1: Check input reading syntax
fmt.Scanln requires a pointer to variable, so &age is needed, not age.
Step 2: Verify other parts
fmt.Println can print variables; age as int is fine; fmt is imported correctly.
Final Answer:
Missing & before variable in fmt.Scanln -> Option B
Quick Check:
Use &variable for input = B [OK]
Hint: Always use & before variable in fmt.Scanln [OK]
Common Mistakes:
Omitting & causes runtime error
Thinking fmt.Println can't print variables
Changing variable type unnecessarily
5. You want to write a Go program that asks the user for two numbers, adds them, and shows the result. Why is input and output required here?
hard
A. Input gets numbers from user; output shows the sum
B. Input stores the sum; output reads numbers
C. Input and output are not needed for calculations
D. Only output is needed to display the sum
Solution
Step 1: Understand input role in the program
The program needs input to get the two numbers from the user to add them.
Step 2: Understand output role in the program
After adding, output is needed to show the sum to the user.
Final Answer:
Input gets numbers from user; output shows the sum -> Option A