Go Output Functions

Go Output Functions – Complete Guide with Examples
In Go (Golang), output functions are used to display data to the console. They help you print text, variables, formatted output, and debug information while running programs.
Go provides output functions mainly through the fmt package.
What Are Output Functions in Go?
Output functions are used to:
Display results
Debug programs
Show messages to users
Print variables and expressions
- Go does not have built-in print keywords.
- All output is handled using the
fmtpackage.
Importing the fmt Package
Before using any output function, import fmt.
fmt.Print() – Basic Output
Prints output without a new line at the end.
Output
- Simple output
- No line break
fmt.Println() – Output with New Line
Prints output with a new line automatically.
Output
- Most commonly used
- Adds space between values
Printing Multiple Values
fmt.Printf() – Formatted Output (Very Important)
Used for formatted printing using format specifiers.
Output
- Powerful
- No automatic newline
Common Format Specifiers
| Specifier | Meaning |
|---|---|
%s | String |
%d | Integer |
%f | Float |
%t | Boolean |
%v | Default value |
%T | Type of value |
%% | Percent sign |
Example
Printing Variables
Printing with New Line Using Printf
-
nadds a new line
fmt.Sprint(), Sprintln(), Sprintf()
These return strings instead of printing.
Example
- Useful for logging
- Useful for storing output
Output with Escape Characters
| Escape | Meaning |
|---|---|
n | New line |
t | Tab |
" | Double quote |
\ | Backslash |
Printing Structs and Arrays
-
%+vprints field names
Debug Printing in Go
- Common during development
- Remove in production
Output vs Return
Bad Practice
Good Practice
- Output is for display
- Return is for logic
Common Mistakes
- Forgetting to import
fmt -
Using wrong format specifier - Expecting
Printfto add newline - Printing instead of returning values
- Overusing debug prints
Best Practices for Go Output
- Use
Println()for normal output - Use
Printf()for formatted output - Use
Sprint()when string is needed - Separate logic and output
- Clean debug prints before deployment
Interview Questions: Go Output Functions
1. Which package is used for output in Go?fmt
2. Difference between Print and Println?Println adds a new line.
3. Does Printf add a new line automatically?
No.
4. What does %v do?
Prints default value.
5. Which function returns a string instead of printing?Sprintf
Summary
- Go uses the
fmtpackage for output -
Print,Println,Printfare core functions - Format specifiers control output
-
Sprintfunctions return strings - Clean output improves debugging
Mastering Go output functions helps you debug faster, display results clearly, and write professional Go programs
