--- /dev/null
+pkg fmt, func Append([]uint8, ...interface{}) []uint8 #47579
+pkg fmt, func Appendf([]uint8, string, ...interface{}) []uint8 #47579
+pkg fmt, func Appendln([]uint8, ...interface{}) []uint8 #47579
}
}
}
+
+// Test the various Append printers. The details are well tested above;
+// here we just make sure the byte slice is updated.
+
+const (
+ appendResult = "hello world, 23"
+ hello = "hello "
+)
+
+func TestAppendf(t *testing.T) {
+ b := make([]byte, 100)
+ b = b[:copy(b, hello)]
+ got := Appendf(b, "world, %d", 23)
+ if string(got) != appendResult {
+ t.Fatalf("Appendf returns %q not %q", got, appendResult)
+ }
+ if &b[0] != &got[0] {
+ t.Fatalf("Appendf allocated a new slice")
+ }
+}
+
+func TestAppend(t *testing.T) {
+ b := make([]byte, 100)
+ b = b[:copy(b, hello)]
+ got := Append(b, "world", ", ", 23)
+ if string(got) != appendResult {
+ t.Fatalf("Append returns %q not %q", got, appendResult)
+ }
+ if &b[0] != &got[0] {
+ t.Fatalf("Append allocated a new slice")
+ }
+}
+
+func TestAppendln(t *testing.T) {
+ b := make([]byte, 100)
+ b = b[:copy(b, hello)]
+ got := Appendln(b, "world,", 23)
+ if string(got) != appendResult+"\n" {
+ t.Fatalf("Appendln returns %q not %q", got, appendResult+"\n")
+ }
+ if &b[0] != &got[0] {
+ t.Fatalf("Appendln allocated a new slice")
+ }
+}
return s
}
+// Appendf formats according to a format specifier, appends the result to the byte
+// slice, and returns the updated slice.
+func Appendf(b []byte, format string, a ...any) []byte {
+ p := newPrinter()
+ p.doPrintf(format, a)
+ b = append(b, p.buf...)
+ p.free()
+ return b
+}
+
// These routines do not take a format string
// Fprint formats using the default formats for its operands and writes to w.
return s
}
+// Append formats using the default formats for its operands, appends the result to
+// the byte slice, and returns the updated slice.
+func Append(b []byte, a ...any) []byte {
+ p := newPrinter()
+ p.doPrint(a)
+ b = append(b, p.buf...)
+ p.free()
+ return b
+}
+
// These routines end in 'ln', do not take a format string,
// always add spaces between operands, and add a newline
// after the last operand.
return s
}
+// Appendln formats using the default formats for its operands, appends the result
+// to the byte slice, and returns the updated slice. Spaces are always added
+// between operands and a newline is appended.
+func Appendln(b []byte, a ...any) []byte {
+ p := newPrinter()
+ p.doPrintln(a)
+ b = append(b, p.buf...)
+ p.free()
+ return b
+}
+
// getField gets the i'th field of the struct value.
// If the field is itself is an interface, return a value for
// the thing inside the interface, not the interface itself.