liveBook · Manning

Format verb

As explained in Go in Action, Second Edition

A format verb is a formatting directive that specifies how a value is converted to text in formatted output.

A format verb is a directive in formatted I/O that controls how a value becomes text. Go uses verbs with fmt functions such as Printf and Sprintf, giving output predictable type-specific representations instead of requiring manual conversion. section 10.1

Format verbs also make operational messages clearer. Annotating an error with surrounding text identifies the failed operation while %s or %v invokes the error's Error method to obtain its message. section 7.6

Common verbs support these practical output tasks:

  • %s formats strings, %v uses a default representation for any value, and %T prints a value's type. section 10.1
  • %d prints decimal integers, while %b, %x, and %X print binary, lowercase hexadecimal, and uppercase hexadecimal forms. section 10.1
  • %f and %.2f format floating-point values with default or specified precision; %q quotes strings. section 10.1
  • %+v includes struct field names, %#v produces Go syntax, and %% emits a literal percent sign. section 10.1

Versioned output and annotated errors

A CLI can combine values with their intended representations: fmt.Printf("%s - version %d", app, version). When a store operation fails, log.Printf("unable to place value: %v", err) preserves the error while adding the failed operation. section 7.6 section 10.1

Table 10.1 Common fmt escape codes

Code

Type

Description

Example output

%v

any

Default format

{Name:myapp}

%+v

struct

Includes field names

{Name:myapp Version:1}

%#v

any

Go syntax representation

main.Config{Name:"myapp"}

%T

any

Type of value

main.Config

%t

bool

Boolean

true

%d

int

Decimal integer

42

%b

int

Binary

101010

%x

int

Hexadecimal (lowercase)

2a

%X

int

Hexadecimal (uppercase)

2A

%f

float

Decimal point (always six digits)

3.141593

%.2f

float

Decimal with precision

3.14

%e

float

Scientific notation

3.141593e+00

%s

string

String

myapp

%q

string

Quoted string

"myapp"

%p

pointer

Pointer address

0xc000010200

%%

literal

Percent sign

%

Use the fmt verb table to compare value types, representations, precision controls, and example output. section 10.1

How Go in Action, Second Edition covers it

How deep it goes
Explains directive categories and concrete representations, but stops short of fmt implementation internals.
Examples
Uses versioned CLI output, numeric and string representations, formatted strings, and annotated key-value-store errors.
What you can do after
Apply it via fmt.Printf, fmt.Sprintf, and logging calls to produce typed output and contextual error messages.
How it connects
Connects formatted output with the fmt package, string construction, error annotation, and structured logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.