Chapter 12: Output Functions

Output Functions” — the exact topic many beginner tutorials (especially W3Schools-style ones) name as a separate section.

“Output Functions” in Go almost always refers to the three core printing functions from the fmt package:

  • fmt.Print()
  • fmt.Println()
  • fmt.Printf()

These are the first output tools every new Go learner uses after seeing Hello World. They appear in almost every introductory tutorial under headings like:

  • “Go Output Functions”
  • “Output in Go”
  • “Printing in Golang”
  • “fmt package basics”

Today I’ll teach them like we’re pair-programming together: differences, when to choose each one, formatting verbs in depth, real patterns, common mistakes, and many examples you can run immediately.

1. Why These Three Functions? (Quick Context)

Go keeps things minimal — no built-in console.log, echo, or System.out.println. Instead, the standard library gives you fmt (format) with exactly these three workhorses for console output (stdout).

All three:

  • Take variadic arguments (…any)
  • Handle basic types automatically
  • Are very fast (used even in production code)

2. Side-by-Side Comparison Table

Function Adds newline at end? Adds space between arguments? Supports format verbs (%s, %d, etc.)? Best used when you want… Typical line count in real code
fmt.Print() No No No (but can fake it) Continuous text without breaks Rare (unless building strings)
fmt.Println() Yes Yes (one space per arg) No Quick debug prints, simple lines Very high (~60–70% of prints)
fmt.Printf() No (add \n yourself) No Yes Controlled / pretty / aligned output High (when formatting needed)

3. Detailed Examples — Run These One by One

Create a file output_functions.go and try each block.

Go

4. Formatting Verbs Cheat Sheet (The Real Power of Printf)

These are the ones you’ll use 95% of the time:

Verb Use for Example Input Output Example Notes / Modifiers
%v Default (“value”) 42, “hi”, true 42 hi true Most general — good fallback
%s String “Webliance” Webliance
%d Integer (decimal) 25, -42 25 -42
%f Float 175.5 175.500000 Default 6 decimals
%.1f Float — 1 decimal 175.5 175.5 Change number after .
%t Boolean true true
%T Type of value 42 int Very useful for debugging
%q Quoted string/rune “hello”, ‘A’ “hello” ‘A’ Good for showing strings safely
%x / %X Hex (lowercase/upper) 255 ff FF Useful for bytes/colors
%% Literal % % To print % sign

Alignment & Width examples (very useful for tables):

Go

5. Bonus Helpers from fmt (Often Mentioned in “Output Functions” Sections)

  • fmt.Sprintf(format, args…) string → formats → returns string (no printing)
Go
  • fmt.Fprintln(os.Stderr, “Critical error!”) → print to stderr (often red in terminals)

6. Common Beginner Mistakes & Tips

  • Forgetting \n in Printf → output sticks together on one line
  • Using Println when you want no space → use Printf instead
  • Too many %v → code becomes hard to read → prefer specific verbs when possible
  • No import “fmt” → compile error (most common first-timer issue)

7. Your Quick Practice Task

Try combining all three in one program:

Go

Which version do you prefer? Why?

Questions now?

  • Want all 30+ formatting verbs explained?
  • How to make colored output (third-party packages)?
  • Output to files / JSON / HTTP responses next?
  • Or jump to input functions (Scan, Scanf, etc.)?

Keep running these examples — seeing output is the best feedback loop in programming! 💻🇮🇳 You’re doing great — let’s keep going! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *