[Go to site: main page, start]

Skip to content

output

Structured, themeable output formatting for CLI tools.

output gives a command-line tool one configured Renderer that speaks both languages a CLI needs at once — human-readable text for a terminal and machine-readable JSON/YAML for a pipeline — plus tables, spinners, progress bars and live status lines. It degrades gracefully to plain output when it is not attached to a terminal.

r := output.New(output.WithWriter(os.Stdout), output.WithFormat(format))

_ = r.Write(result, func(w io.Writer) {
    fmt.Fprintf(w, "%s is up to date (%s)\n", result.Name, result.Version)
})

Highlights

  • Configure once, render anywhere. The writer, format, theme and interactivity are fixed on the Renderer; every method reads them. No globals — trivially testable with a bytes.Buffer.
  • One façade, six formats. Write, Table, Render, Emit/EmitError, Status, Progress and Spin share the same configured destination and format.
  • Unicode-correct. Widths and truncation use display width, so CJK, emoji and combining characters never corrupt a table or a status line.
  • Framework-free core. Importing output never pulls cobra or pflag; the cobra wiring is the opt-in output/cobra subpackage.

Where next


Part of the phpboyscout Go toolkit — small, framework-free Go modules extracted from go-tool-base.

Further reading

The blog carries a curated route through this subject: Building a command-line tool in Go collects everything written about it, ordered so you can start at the beginning rather than newest-first.

Ask phpbotscout

phpbotscout

He answers questions about the projects over on the Discord, citing the docs where they already cover it, and offering to raise an issue where they don't. Bring a bug, an idea, or a questionable engineering decision.

Join the Discord