liveBook · Manning

Fmt

As explained in Go in Action, Second Edition

The fmt package is a Go standard-library package that provides formatted input and output operations.

fmt is Go’s standard-library package for formatted input and output. Import it after the package declaration, then call exported functions with the package-qualified form, such as fmt.Println. section 2.4.6 section 2.4.11

Choose the function that matches the output or input job:

  • Use Print for adjacent output, Println for output with a trailing newline, and Printf for strings controlled by formatting verbs. section 10.1
  • Use Sprint to build a string by concatenating arguments and Sprintf to build one from a format string. section 10.1
  • Use Scanln for simple line input and Errorf for formatted errors that can be printed or returned. section 10.1

Formatting a conversion CLI

A small JSON/XML conversion tool can report its identity and version with `fmt.Printf("%s - version %d", app, version)`, prompt with `fmt.Print("Input json: ")`, read with `fmt.Scanln(&input)`, and reject empty input using `fmt.Errorf("invalid json: empty input")`. Common verbs include `%s` for strings, `%d` for decimal integers, `%v` for default values, `%T` for types, and `%.2f` for controlled float precision. 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 formatting-verbs table to select representations for values, structs, types, booleans, integers, floats, strings, pointers, and literal percent signs. section 10.1

Listing 10.4 main.go: Reading input and creating formatted errors
func main() {
    ...

    var input string
    fmt.Print("Input json: ") #1
    fmt.Scanln(&input) #2

    if input == "" {
        err := fmt.Errorf("invalid json: empty input") #3
        fmt.Println(err) #4
        os.Exit(1)
    }

    fmt.Printf("Received: %s\n", input)
}

Use the runnable pattern to connect a prompt, Scanln input, empty-input validation, Errorf construction, and formatted confirmation. section 10.1

How Go in Action, Second Edition covers it

How deep it goes
Explains core printing, formatting verbs, string builders, input, and formatted errors, while deferring advanced logging options.
Examples
Uses word-count output, a versioned JSON/XML conversion CLI, interactive JSON input, formatting verbs, and printed key-value-store errors.
What it assumes
Assumes package declarations and basic Go code, while teaching the import declaration and package-qualified calls needed to use fmt.
What you can do after
Apply it via fmt functions to print, format strings, read simple input, and create or display formatted errors.
How it connects
Connects package imports, exported identifiers, formatted I/O, errors, logging, and command-line application architecture.

Also explained in

Compare how 3 books explain fmt →

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.