Chapter 11: Go Output

Go Output” — how you actually show things to the user (or logs, files, etc.) in Go.

“Go Output” usually means printing / displaying data to the console (standard output = stdout), especially for beginners.

In almost every Go tutorial (W3Schools, freeCodeCamp style, YouTube beginner series, official Tour examples), the section called “Go Output” or “Output Functions” teaches:

  • The three main functions from the fmt package: Print()Println()Printf()

These are the first output tools every beginner learns after fmt.Println(“Hello World”).

(There are many other ways to output — files, HTTP responses, JSON, logs — but “Go Output” in beginner context = these three + basic formatting.)

I’ll explain like your teacher sitting next to you: why each exists, differences, when to use which, formatting verbs (%v, %s, %d etc.), common patterns, and tons of runnable examples.

1. The Three Main Output Functions (from import “fmt”)

All live in the fmt package (short for “format”) — import it every time you want output.

Function What it does Adds newline? Supports formatting verbs? Most used for?
fmt.Print() Prints arguments as-is, no extra space/newline No No (but can use Printf style if you want) Continuous text without breaks
fmt.Println() Prints arguments + space between + newline at end Yes No Quick debug, simple lines
fmt.Printf() Formatted printing — uses verbs like %s, %d No (unless you add \n) Yes Pretty / controlled output

2. Basic Examples — Copy-Paste & Run These!

Create output.go:

Go

Run:

Bash

Output:

text

See the differences?

3. The Real Power: Formatting Verbs in Printf / Sprintf

Printf uses verbs (placeholders) starting with %.

Common ones beginners use first:

Verb Meaning Example value Output example
%v Default format (value) 42, “hi”, true 42 hi true
%s String “Webliance” Webliance
%d Integer (decimal) 25 25
%f Float 175.5 175.500000
%.1f Float with 1 decimal 175.5 175.5
%t Boolean true true
%T Type of value 42 int
%q Quoted string / char “hello” “hello”
%x Hexadecimal 255 ff
%% Literal % sign %

More examples:

Go

4. Other Useful fmt Output Helpers

  • fmt.Sprintf() → formats → returns string (no print)
Go
  • fmt.Fprintln(os.Stderr, “Error!”) → output to stderr (red in many terminals)

5. Common Patterns Beginners Use

Debug style:

Go

Table-like output:

Go

Error handling classic:

Go

6. Quick Tips & Gotchas (2026 Style)

  • Always import “fmt” — no output without it (except panic, log, etc.)
  • Println adds space between args — sometimes annoying:
Go
  • No automatic JSON pretty-print — use encoding/json + json.MarshalIndent for that.
  • For colors in terminal → third-party like github.com/fatih/color (not in stdlib)
  • go fmt doesn’t touch strings — your formatting stays safe.

Your Mini Exercise

Write this in output.go and run:

Go

Add one more line with %q on a string.

Any confusion?

  • Want deeper on all verbs (%p pointers, %b binary, etc.)?
  • How to output to files / JSON / HTTP?
  • Or next: functions? slices? structs?

You’re doing awesome — keep running code and seeing output! 💻🇮🇳 Let’s go! 🚀

You may also like...

Leave a Reply

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