Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. Format Verbs

Format Verbs

Go's fmt package uses format verbs to control how values are printed. %v is the default format that works with any type, and there are also type-specific verbs available.

Syntax

// Basic usage (used inside fmt.Printf, fmt.Sprintf)
fmt.Printf("%v\n", value)       // Default format
fmt.Printf("%d\n", integer)     // Decimal integer
fmt.Printf("%s\n", str)         // String
fmt.Printf("%f\n", floatNum)    // Floating-point number

// Specifying width and precision
fmt.Printf("%10d\n", 42)        // Width 10, right-aligned
fmt.Printf("%-10d\n", 42)       // Width 10, left-aligned
fmt.Printf("%010d\n", 42)       // Width 10, zero-padded
fmt.Printf("%.2f\n", 3.14159)   // 2 decimal places
fmt.Printf("%8.2f\n", 3.14)     // Width 8, 2 decimal places

Format Verb Reference

VerbDescription
%vDefault format. Prints values in a natural representation for their type.
%+vLike %v, but includes field names when printing structs.
%#vPrints the value in Go syntax (with package name and type name).
%TPrints the type name of the value.
%dPrints an integer in decimal (base 10).
%bPrints an integer in binary (base 2).
%oPrints an integer in octal (base 8).
%x / %XPrints an integer in hexadecimal (lowercase / uppercase).
%e / %EPrints a floating-point number in scientific notation.
%fPrints a floating-point number in decimal notation (default precision: 6 digits).
%g / %GUses %e or %f, whichever is more compact for the value.
%sPrints a string or byte slice.
%qPrints a string as a double-quoted Go string literal.
%cPrints the Unicode character corresponding to the integer value.
%tPrints a boolean as true or false.
%pPrints a pointer as a hexadecimal address.
%%Prints a literal percent sign %.

Sample Code

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    // General-purpose verbs
    p := Person{"Alice", 30}
    fmt.Printf("%v\n", p)    // {Alice 30}
    fmt.Printf("%+v\n", p)   // {Name:Alice Age:30}
    fmt.Printf("%#v\n", p)   // main.Person{Name:"Alice", Age:30}
    fmt.Printf("%T\n", p)    // main.Person

    fmt.Println()

    // Integer in different bases
    n := 255
    fmt.Printf("Decimal: %d\n", n)
    fmt.Printf("Binary: %b\n", n)
    fmt.Printf("Octal: %o\n", n)
    fmt.Printf("Hex (lower): %x\n", n)
    fmt.Printf("Hex (upper): %X\n", n)

    fmt.Println()

    // Floating-point formatting
    f := 3.14159265
    fmt.Printf("Default: %f\n", f)
    fmt.Printf("Precision: %.2f\n", f)
    fmt.Printf("Scientific: %e\n", f)
    fmt.Printf("Compact: %g\n", f)

    fmt.Println()

    // Width and alignment
    fmt.Printf("|%10d|\n", 42)    // Right-aligned
    fmt.Printf("|%-10d|\n", 42)   // Left-aligned
    fmt.Printf("|%010d|\n", 42)   // Zero-padded

    // Special verbs
    fmt.Printf("%q\n", "Hello, 世界") // "Hello, 世界" (escaped string literal)
    fmt.Printf("%c\n", 65)           // A (character code 65)
    fmt.Printf("100%%\n")            // 100% (%% prints a literal percent sign)
}

Notes

Format verbs are used everywhere from debug output to production logging. Use %v or %#v to inspect values of unknown type. For numeric formatting, combining %d or %f with width and precision flags is very powerful. To customize formatting for your own types, implement the String() string method (the Stringer interface) — it will be called automatically by %v and %s.

If you use a verb that does not match the value's type — for example, %s with an integer or %d with a string — Go prints a warning string like %!s(int=42). Run go vet to catch these mismatches before compiling.

For details on fmt.Printf(), see fmt.Println() / Printf(). For string formatting, see fmt.Sprintf() / Fprintf().

If you find any errors or copyright issues, please .