Go Output Functions

Su Suriya Ravichandran Updated 14 Sep 2026
4 min read ·Lesson 7 of 12

Go's standard library provides output through the fmt package. Three functions cover almost every case you'll need:

  • fmt.Print() — prints values with minimal formatting.
  • fmt.Println() — prints values with automatic spacing and a trailing newline.
  • fmt.Printf() — prints values using a format string with placeholders, for precise control over output.

fmt.Print() — Basic Output

Print() prints its arguments using their default format. It adds a space between two operands only when neither is a string, and it never adds a trailing newline.

package main

import "fmt"

func main() {
    message1 := "Good"
    message2 := "Morning"

    fmt.Print(message1)
    fmt.Print(message2)
}

Expected output:

GoodMorning

Adding Line Breaks with \n

To force a new line, include the newline escape sequence \n inside the string:

package main

import "fmt"

func main() {
    fmt.Print("Welcome\n")
    fmt.Print("To Go Programming\n")
}

Expected output:

Welcome
To Go Programming

Printing Multiple Values with Print()

package main

import "fmt"

func main() {
    name := "Anita"
    age := 22

    fmt.Print("Name: ", name, "\nAge: ", age)
}

Expected output:

Name: Anita
Age: 22

Note on spacing: Print()'s rule is easy to misremember — it adds a space between two adjacent operands only when neither is a string. In the example above, "Name: " and name are both strings, so no extra space is inserted between them (which is why the string already ends with a space); but between "\nAge: " and age (a string and an int), a space is added automatically. This inconsistency is exactly why Println() is preferred for most everyday printing — it removes the guesswork.

fmt.Println() — Line-Based Output

Println() behaves like Print(), but with two differences that make it the default choice for simple output:

  • It always inserts a space between arguments, regardless of type.
  • It always adds a newline at the end.
package main

import "fmt"

func main() {
    city := "Chennai"
    temperature := 32

    fmt.Println("City:", city, "Temperature:", temperature)
}

Expected output:

City: Chennai Temperature: 32

fmt.Printf() — Formatted Output

Printf() takes a format string containing verbs — placeholders starting with % — and substitutes each one with the corresponding argument, in order. Unlike Print and Println, Printf() does not add a trailing newline automatically; you must include \n yourself.

package main

import "fmt"

func main() {
    language := "Go"
    year := 2009

    fmt.Printf("%s was released in %d\n", language, year)
}

Expected output:

Go was released in 2009

When to Use Each Function

  • Use Print() for quick, minimally formatted output — rare in practice because of its inconsistent spacing rule.
  • Use Println() for simple output where you just want values separated by spaces and ending in a newline.
  • Use Printf() whenever you need control over formatting — padding, decimal precision, type inspection, or building structured log lines.

Printf() Formatting Verbs

Formatting verbs are the placeholders inside a Printf() format string. Each verb expects a matching argument of a compatible type; a mismatch (say, using %d with a string) produces a visible error marker in the output rather than a silent failure, which is one of Go's safer design choices.

General-Purpose Verbs

VerbDescription
%vDefault format for the value
%#vGo-syntax representation of the value
%TThe value's type
%%A literal % character
package main

import "fmt"

func main() {
    value := 42.75
    text := "GoLang"

    fmt.Printf("Value: %v\n", value)
    fmt.Printf("Go-syntax: %#v\n", value)
    fmt.Printf("Type: %T\n", value)
    fmt.Printf("Completion: %v%%\n", 85)

    fmt.Printf("Text: %v | %#v | %T\n", text, text, text)
}

Integer Verbs

VerbDescription
%bBinary (base 2)
%dDecimal (base 10)
%+dDecimal, always showing the sign
%oOctal
%OOctal, with a 0o prefix
%x / %XHexadecimal (lowercase / uppercase)
%#xHexadecimal with a 0x prefix
%4dMinimum width 4, right-aligned
%-4dMinimum width 4, left-aligned
%04dMinimum width 4, zero-padded
package main

import "fmt"

func main() {
    number := 27

    fmt.Printf("Binary: %b\n", number)
    fmt.Printf("Decimal: %d\n", number)
    fmt.Printf("Signed: %+d\n", number)
    fmt.Printf("Octal: %o\n", number)
    fmt.Printf("Octal (prefixed): %O\n", number)
    fmt.Printf("Hex (lower): %x\n", number)
    fmt.Printf("Hex (upper): %X\n", number)
    fmt.Printf("Hex (with prefix): %#x\n", number)

    fmt.Printf("Right padded: %4d\n", number)
    fmt.Printf("Left padded: %-4d\n", number)
    fmt.Printf("Zero padded: %04d\n", number)
}

String Verbs

VerbDescription
%sPlain string
%qDouble-quoted, escaped string
%8sRight-aligned, minimum width 8
%-8sLeft-aligned, minimum width 8
%xHex-encoded bytes of the string
% xHex-encoded bytes, space-separated
package main

import "fmt"

func main() {
    word := "Code"

    fmt.Printf("Plain: %s\n", word)
    fmt.Printf("Quoted: %q\n", word)
    fmt.Printf("Right aligned: %8s\n", word)
    fmt.Printf("Left aligned: %-8s\n", word)
    fmt.Printf("Hex: %x\n", word)
    fmt.Printf("Hex (spaced): % x\n", word)
}

Boolean Verb

VerbDescription
%ttrue or false
package main

import "fmt"

func main() {
    isLoggedIn := true
    isAdmin := false

    fmt.Printf("Logged in: %t\n", isLoggedIn)
    fmt.Printf("Admin: %t\n", isAdmin)
}

Floating-Point Verbs

VerbDescription
%eScientific notation
%fStandard decimal notation
%.2fFixed to 2 decimal places
%6.2fMinimum width 6, 2 decimal places
%gCompact format — %e for very large/small numbers, %f otherwise
package main

import "fmt"

func main() {
    pi := 3.14159

    fmt.Printf("Scientific: %e\n", pi)
    fmt.Printf("Decimal: %f\n", pi)
    fmt.Printf("Rounded: %.2f\n", pi)
    fmt.Printf("Width + precision: %6.2f\n", pi)
    fmt.Printf("Compact: %g\n", pi)
}

Expected output:

Scientific: 3.141590e+00
Decimal: 3.141590
Rounded: 3.14
Width + precision:   3.14
Compact: 3.14159

Common mistake: forgetting that %d is for integers only — passing a float64 to %d produces an error marker like %!d(float64=3.14) in the output instead of a number. Use %f, %g, or %v for floating-point values.

0 Comments

Reviewed before they appear

No comments yet.

Go Lang
Ask about this post
AI Ask about this post

Ask questions about Go Output Functions and get answers drawn from it.

Signed-in readers only.