We use fmt.Println to show messages or results on the screen. It helps us see what our program is doing.
Output using fmt.Println in Go
Introduction
Syntax
Go
fmt.Println(value1, value2, ...)
fmt.Println prints each value separated by a space and adds a new line at the end.
You can print many values at once by separating them with commas.
Examples
Go
fmt.Println("Hello, world!")Go
fmt.Println(10, 20, 30)
Go
fmt.Println("Sum:", 5+3)
Sample Program
This program prints three lines: a welcome message, a number with a label, and a goodbye message.
Go
package main import "fmt" func main() { fmt.Println("Welcome to Go programming!") fmt.Println("The answer is", 42) fmt.Println("Goodbye!") }
Important Notes
Always import fmt to use fmt.Println.
fmt.Println automatically adds a new line after printing.
Use commas to print multiple values with spaces in between.
Summary
fmt.Println prints text or values to the screen with a new line.
You can print many things at once by separating them with commas.
It is useful for showing messages, results, or debugging information.
Practice
1. What does the
fmt.Println function do in Go?easy
Solution
Step 1: Understand the purpose of fmt.Println
The functionfmt.Printlnis 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 DQuick 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
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 AQuick Check:
Strings need double quotes in fmt.Println [OK]
Hint: Use double quotes for strings in fmt.Println [OK]
Common Mistakes:
- Using single quotes for strings
- Omitting quotes around text
- Forgetting commas inside print
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
fmt.Println("Go", "is", "fun")
}medium
Solution
Step 1: Understand fmt.Println with multiple arguments
When multiple values are separated by commas, fmt.Println prints them separated by spaces automatically.Step 2: Analyze the output
The output will be the words separated by spaces: Go is fun, followed by a new line.Final Answer:
Go is fun -> Option AQuick Check:
fmt.Println adds spaces between arguments [OK]
Hint: Multiple arguments print with spaces automatically [OK]
Common Mistakes:
- Expecting no spaces between words
- Thinking commas print literally
- Confusing output with quotes
4. Find the error in this Go code snippet:
package main
import "fmt"
func main() {
fmt.Println("Hello" + "World")
}medium
Solution
Step 1: Check string concatenation in Go
Using + between strings concatenates them without spaces.Step 2: Verify fmt.Println usage
fmt.Println can print the concatenated string without error.Final Answer:
No error, it prints HelloWorld -> Option CQuick Check:
String + concatenates; fmt.Println prints result [OK]
Hint: String + joins text; no comma needed inside fmt.Println [OK]
Common Mistakes:
- Thinking + causes syntax error in print
- Expecting space between Hello and World
- Confusing + with comma separator
5. You want to print the numbers 1, 2, and 3 on the same line separated by spaces using
fmt.Println. Which code snippet achieves this correctly?hard
Solution
Step 1: Understand printing multiple values
Using commas in fmt.Println prints each argument separated by spaces automatically.Step 2: Analyze each option
fmt.Println(1 2 3)causes syntax error (missing commas or operators).fmt.Println("1" "2" "3")concatenates adjacent strings to "123" without spaces.fmt.Println("1", "2", "3")prints 1 2 3 with spaces.fmt.Println(1 + 2 + 3)prints 6.Step 3: Choose the best for printing numbers as strings separated by spaces
fmt.Println("1", "2", "3")prints the numbers as strings separated by spaces, exactly as requested.Final Answer:
fmt.Println("1", "2", "3") -> Option BQuick Check:
Separate strings with commas to print spaced values [OK]
Hint: Use commas to separate values for spaced output [OK]
Common Mistakes:
- Using + to join numbers (adds them instead)
- Printing a single string instead of separate values
- Confusing string and integer printing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
