Skip to main content

Output

Two Families of Printing in Go

  1. print() and println() → low-level, built-in functions (from the Go runtime, not fmt package).

  2. fmt.Printf() and friends → from the fmt package, much more powerful and commonly used

print()

  • Purpose: Print text or values in a basic way.
  • No formatting: It just writes values as-is.
  • No automatic newline: You control spacing manually.
  • Arguments: Can take multiple arguments, prints them directly.
  • Not recommended for production code (meant for quick debugging).

println()

  • Same as print(), but automatically adds a newline (\n) at the end.
  • Still no fancy formatting, just prints each argument separated by a space.

With multiple arguments:

println("Score:", 100)  // Score: 100 - Notice the space between arguments.

fmt.Printf()

  • Comes from the fmt package.
  • Formatted printing — you can control how values are shown.
  • Requires format verbs (placeholders like %d, %s, %f) inside a string.
  • No automatic newline — you must add \n if you want one.

Common verbs:

  • %d → integer
  • %s → string
  • %f → floating point
  • %t → boolean
  • %v → value (automatic format)
  • %T → type of value
fmt.Printf("Name: %s, Age: %d\n", "Alice", 25) // Name: Alice, Age: 25

Comparison of Outputs

Featureprint()println()fmt.Printf()
Package neededNo (built-in)No (built-in)Yes (import "fmt")
Auto newlineNoYesNo (unless \n added)
Formatting controlNoNoYes (format verbs)
Multiple argumentsYes (no format)Yes (space-separated)Yes (matches placeholders)
Recommended for real apps❌ Debug only❌ Debug only✅ Yes