liveBook · Manning

Fmt.Fprintf

As explained in Go in Action, Second Edition

fmt.Fprintf is a Go standard-library function that formats text according to a format string and writes it to an io.Writer.

fmt.Fprintf formats text from a format string and writes the result to any io.Writer. Unlike fmt.Printf, it does not require standard output, so the same output logic can target files, buffers, screens, or network connections. section 10.3

This decouples formatting from the destination. A process can accept an io.Writer and send status or error messages wherever the caller chooses, supporting flexible CLI tools and logging systems. section 10.3

Writing a status message

Set writer to os.Stdout or another io.Writer, then call fmt.Fprintf(writer, "Output written to: %s\n", outputFile). The same call can write to a file, buffer, or other supported destination. section 10.3

How Go in Action, Second Edition covers it

How deep it goes
Explains the destination distinction from fmt.Printf and demonstrates one formatted write, stopping short of deeper formatting details.
Examples
Demonstrates writing an output-file status message through os.Stdout stored in an io.Writer.
What it assumes
Assumes familiarity with Go packages and interfaces while establishing io.Writer as the output abstraction.
What you can do after
Apply it via Go's fmt package to send formatted messages through interchangeable io.Writer destinations.
How it connects
Connects formatted output with io.Writer, fmt.Printf, files, buffers, network connections, and composable I/O interfaces.

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.