fmt: document space behavior of Append

Also, introduce the {Print,Fprint,Sprint,Append}{,f,ln}
cross product of functions at the top of the docs.

Fixes #74656

Change-Id: I85a156cd545ca866e579d8020ddf165cd4bcb26f
Reviewed-on: https://go-review.googlesource.com/c/go/+/688877
Reviewed-by: Rob Pike <r@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Alan Donovan 2025-07-18 14:57:38 -04:00
parent c079dd13c0
commit 67d4a28707
2 changed files with 14 additions and 1 deletions

View file

@ -9,6 +9,18 @@ are simpler.
# Printing # Printing
There are four families of printing functions defined by their output destination.
[Print], [Println] and [Printf] write to [os.Stdout];
[Sprint], [Sprintln] and [Sprintf] return a string;
[Fprint], [Fprintln] and [Fprintf] write to an [io.Writer]; and
[Append], [Appendln] and [Appendf] append the output to a byte slice.
The functions within each family do the formatting according to the end of the name.
Print, Sprint, Fprint and Append use the default format for each argument,
adding a space between operands when neither is a string.
Println, Sprintln, Fprintln and Appendln always add spaces and append a newline.
Printf, Sprintf, Fprintf and Appendf use a sequence of "verbs" to control the formatting.
The verbs: The verbs:
General: General:
@ -222,7 +234,7 @@ formatting methods such as Error or String on unexported fields.
# Explicit argument indexes # Explicit argument indexes
In [Printf], [Sprintf], and [Fprintf], the default behavior is for each In [Printf], [Sprintf], [Fprintf], and [Appendf], the default behavior is for each
formatting verb to format successive arguments passed in the call. formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation nth one-indexed argument is to be formatted instead. The same notation

View file

@ -284,6 +284,7 @@ func Sprint(a ...any) string {
// Append formats using the default formats for its operands, appends the result to // Append formats using the default formats for its operands, appends the result to
// the byte slice, and returns the updated slice. // the byte slice, and returns the updated slice.
// Spaces are added between operands when neither is a string.
func Append(b []byte, a ...any) []byte { func Append(b []byte, a ...any) []byte {
p := newPrinter() p := newPrinter()
p.doPrint(a) p.doPrint(a)