📖 Scenario: You are learning how to show messages on the screen using Go programming language. This is like writing a note that everyone can see when the program runs.
🎯 Goal: You will write a simple Go program that prints a greeting message using fmt.Println.
📋 What You'll Learn
Create a variable to hold a greeting message
Use fmt.Println to display the greeting message
💡 Why This Matters
🌍 Real World
Printing messages is the first step in making programs that talk to users or show information.
💼 Career
Knowing how to output text is essential for debugging and user interaction in software development.
Progress0 / 4 steps
1
Create a greeting message variable
Create a variable called message and set it to the string "Hello, Go learner!".
Go
Hint
Use := to create and assign a variable in one step.
2
Import the fmt package
Make sure the fmt package is imported at the top of your program with import "fmt".
Go
Hint
The fmt package is needed to use fmt.Println.
3
Print the greeting message
Use fmt.Println to print the variable message inside the main function.
Go
Hint
Write fmt.Println(message) to show the message on the screen.
4
Run the program to see the output
Run the program and make sure it prints exactly Hello, Go learner! on the screen.
Go
Hint
Use the command go run filename.go to run your program and see the message.
Practice
(1/5)
1. What does the fmt.Println function do in Go?
easy
A. Creates a new package
B. Reads input from the user
C. Declares a new variable
D. Prints text or values to the screen with a new line
Solution
Step 1: Understand the purpose of fmt.Println
The function fmt.Println is used to display output on the screen, automatically adding a new line after the output.
Step 2: Differentiate from other functions
It does not read input, declare variables, or create packages; it only prints output.
Final Answer:
Prints text or values to the screen with a new line -> Option D
Quick Check:
fmt.Println prints output with newline [OK]
Hint: Remember: println means print with a new line [OK]
Common Mistakes:
Confusing fmt.Println with input functions
Thinking it declares variables
Assuming it creates packages
2. Which of the following is the correct way to print "Hello, Go!" using fmt.Println?
easy
A. fmt.Println("Hello, Go!")
B. fmt.Println('Hello, Go!')
C. fmt.Println(Hello, Go!)
D. fmt.Println(Hello Go)
Solution
Step 1: Check string syntax in Go
Strings must be enclosed in double quotes " ". Single quotes ' ' are for single characters.
Step 2: Validate the options
fmt.Println("Hello, Go!") uses double quotes correctly. Options A, B, and C either use single quotes or no quotes, which causes errors.
Final Answer:
fmt.Println("Hello, Go!") -> Option A
Quick Check:
Strings need double quotes in fmt.Println [OK]
Hint: Use double quotes for strings in fmt.Println [OK]