Go Output Functions

Go Tutorial

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

  1.  Go does not have built-in print keywords.
  2. All output is handled using the fmt package.

 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

Hello Go
  •  Simple output
  •  No line break

fmt.Println() – Output with New Line

Prints output with a new line automatically.

Output

Hello Go
Welcome
  • Most commonly used
  •  Adds space between values

Printing Multiple Values

fmt.Printf() – Formatted Output (Very Important)

Used for formatted printing using format specifiers.


 

Output

Name: Amit, Age: 25
  •  Powerful
  •  No automatic newline

 Common Format Specifiers

SpecifierMeaning
%sString
%dInteger
%fFloat
%tBoolean
%vDefault value
%TType of value
%%Percent sign

Example

 Printing Variables


 

 Printing with New Line Using Printf

  • n adds 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

EscapeMeaning
nNew line
tTab
"Double quote
\Backslash

Printing Structs and Arrays


 

  • %+v prints 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 Printf to 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 fmt package for output
  • Print, Println, Printf are core functions
  • Format specifiers control output
  • Sprint functions return strings
  •  Clean output improves debugging

Mastering Go output functions helps you debug faster, display results clearly, and write professional Go programs

You may also like...