Skip to content

Commit 67d4a28

Browse files
committed
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>
1 parent c079dd1 commit 67d4a28

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/fmt/doc.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ are simpler.
99
1010
# Printing
1111
12+
There are four families of printing functions defined by their output destination.
13+
[Print], [Println] and [Printf] write to [os.Stdout];
14+
[Sprint], [Sprintln] and [Sprintf] return a string;
15+
[Fprint], [Fprintln] and [Fprintf] write to an [io.Writer]; and
16+
[Append], [Appendln] and [Appendf] append the output to a byte slice.
17+
18+
The functions within each family do the formatting according to the end of the name.
19+
Print, Sprint, Fprint and Append use the default format for each argument,
20+
adding a space between operands when neither is a string.
21+
Println, Sprintln, Fprintln and Appendln always add spaces and append a newline.
22+
Printf, Sprintf, Fprintf and Appendf use a sequence of "verbs" to control the formatting.
23+
1224
The verbs:
1325
1426
General:
@@ -222,7 +234,7 @@ formatting methods such as Error or String on unexported fields.
222234
223235
# Explicit argument indexes
224236
225-
In [Printf], [Sprintf], and [Fprintf], the default behavior is for each
237+
In [Printf], [Sprintf], [Fprintf], and [Appendf], the default behavior is for each
226238
formatting verb to format successive arguments passed in the call.
227239
However, the notation [n] immediately before the verb indicates that the
228240
nth one-indexed argument is to be formatted instead. The same notation

src/fmt/print.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ func Sprint(a ...any) string {
284284

285285
// Append formats using the default formats for its operands, appends the result to
286286
// the byte slice, and returns the updated slice.
287+
// Spaces are added between operands when neither is a string.
287288
func Append(b []byte, a ...any) []byte {
288289
p := newPrinter()
289290
p.doPrint(a)

0 commit comments

Comments
 (0)