typestar

fmt verbs in Go

The verb decides the rendering: value, quoted, type, or Go syntax.

type Run struct {
    Lang string
    TPM  float64
}

func verbs() []string {
    run := Run{Lang: "go", TPM: 104.567}
    return []string{
        fmt.Sprintf("%v", run),
        fmt.Sprintf("%+v", run),
        fmt.Sprintf("%#v", run),
        fmt.Sprintf("%T", run),
        fmt.Sprintf("%q|%8.2f|%-8s|", run.Lang, run.TPM, run.Lang),
        fmt.Sprintf("%d %05d %x %b", 42, 42, 255, 5),
    }
}

How it works

  1. %v is the default, %+v adds struct field names.
  2. %q quotes, %T prints the type, %#v prints Go syntax.
  3. Width and precision go between the percent and the verb.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
331
Tokens
106
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 67 seconds.

Type this snippet

Step 4 of 5 in Strings & formatting, step 28 of 30 in Language basics.

← Previous Next →