Printing Data in Go Lang

Learn the complete guide to printing data in Go Lang using fmt.Print, fmt.Println, and fmt.Printf functions. Master formatting verbs, understand standard output streams, and discover when to use each function with practical examples and real-world usage patterns.

The go print statement is fundamental to displaying output in Go programs. When people say “read and write on screen,” they usually mean:

  • Write output to the terminal (what users see)
  • Read input from the terminal (what users type)

In Go, this maps to three standard streams:

StreamWhat it isGo handleTypical use
stdinStandard inputos.StdinRead user input
stdoutStandard outputos.StdoutPrint normal output
stderrStandard erroros.StderrPrint errors/diagnostics

Why does this matter?

Because CLI tools often redirect these streams:

  • myapp > output.txt redirects stdout to a file
  • Errors should still appear on screen via stderr
  • cat input.txt | myapp provides stdin from another program

Go Print Statement: fmt.Print, fmt.Println, fmt.Printf

All three functions write to standard output (stdout) by default. They’re part of Go’s fmt package, which is the standard way to print text and values.

At a high level:

  • Print → prints values as-is, no newline
  • Println → prints values with spaces between them, adds newline
  • Printf → prints using a format string with placeholders

fmt.Print(...)

What it does

  • Prints the provided arguments.
  • Does not add a newline at the end.
  • If you pass multiple arguments, it prints them without guaranteed spacing (so add spaces yourself if needed).

Examples

package main

import "fmt"

func main() {
	fmt.Print("Hello")
	fmt.Print("World")
}

Multiple values

fmt.Print("Age: ", 35, ", Active: ", true)

When to use Print

  • When you want to build output without auto-newlines, e.g. prompts: fmt.Print("Enter your name: ")
  • When you want tight control over line breaks.

2) fmt.Println(...)

What it does

  • Prints the arguments.
  • Automatically inserts a space between arguments (when there are multiple).
  • Always appends a newline at the end.

Examples

package main

import "fmt"

func main() {
	fmt.Println("Hello")
	fmt.Println("Hello", "World")
	fmt.Println("Age:", 35, "Active:", true)
}

When to use Println

  • Most general-purpose printing.
  • Logging-style prints during learning/debugging.
  • Quick output where formatting doesn’t need to be strict.

3) fmt.Printf(format, values...)

What it does

  • Prints using a format string and corresponding values.
  • Does not automatically add a newline unless you include \n.
  • Best for structured output and consistent formatting.

Example

package main

import "fmt"

func main() {
	name := "Siva"
	age := 35

	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Common formatting verbs

General-purpose

  • %v → default value (best “print anything”)
  • %T → prints the type
  • %% → prints a literal %
fmt.Printf("Value=%v Type=%T\n", 42, 42)

Strings

  • %s → string
  • %q → quoted string
fmt.Printf("Name: %s\n", "Go")
fmt.Printf("Name: %q\n", "Go")

Integers

  • %d → decimal integer
  • %b → binary
  • %x → hex (lowercase)
fmt.Printf("Dec=%d Bin=%b Hex=%x\n", 10, 10, 10)

Floats

  • %f → float with decimals
  • %.2f → 2 decimal places
fmt.Printf("Pi=%.2f\n", 3.14159)

Booleans

  • %t → true/false
fmt.Printf("Enabled=%t\n", true)

Precision, width, and alignment

Width and alignment

  • %10s → right-align in 10-width
  • %-10s → left-align in 10-width
fmt.Printf("|%10s|\n", "Go")
fmt.Printf("|%-10s|\n", "Go")

Output:

|        Go|
|Go        |

Numeric width & padding

fmt.Printf("|%5d|\n", 42)
fmt.Printf("|%05d|\n", 42)

Output:

|   42|
|00042|

Real-world usage patterns

1) CLI prompt (best with Print)

fmt.Print("Enter age: ")

Reason: prompt should stay on the same line while user types.

2) Quick debug prints (best with Println)

fmt.Println("user:", user, "err:", err)

3) Table-like output (best with Printf)

fmt.Printf("%-15s %5d\n", "Apples", 10)
fmt.Printf("%-15s %5d\n", "Oranges", 25)

Output:

Apples             10
Oranges            25

Under the hood

All these functions ultimately write to stdout (unless you use fmt.Fprint* variants).

  • fmt.Print(...) ≈ “print values”
  • fmt.Println(...) ≈ “print values + spaces + newline”
  • fmt.Printf(...) ≈ “print with a template string”

If you ever need to print to a different destination (like a file or stderr), you use:

  • fmt.Fprint(writer, ...)
  • fmt.Fprintln(writer, ...)
  • fmt.Fprintf(writer, format, ...)

Example:

fmt.Fprintln(os.Stderr, "Something failed")

Quick summary

  • Use fmt.Print for prompts or when you don’t want a newline.
  • Use fmt.Println for most simple prints (space-separated + newline).
  • Use fmt.Printf when you need structured output, alignment, or formatting control (remember \n).

Example:

Samples can be found here : srnyapathi/golab_fmt_example

srnyapathi
srnyapathi
Articles: 41

One comment

Comments are closed.