fmt.Append formats spaces between non-string operands, but doesn't mentiond this in its doc:
Append formats using the default formats for its operands, appends the result to the byte slice, and returns the updated slice.
https://github.com/golang/go/blob/66536242fce34787230c42078a7bbd373ef8dcb0/src/fmt/print.go#L285-L286
At the same time fmt.Sprint() documentation is very specific about it:
Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
As a result, it makes false impression that formatting will be different, but it will format values the same way, only buffer operations are different:
func Sprint(a ...any) string {
p := newPrinter()
p.doPrint(a)
s := string(p.buf)
p.free()
return s
}
func Append(b []byte, a ...any) []byte {
p := newPrinter()
p.doPrint(a)
b = append(b, p.buf...)
p.free()
return b
}
Comment From: gabyhelp
Related Issues
- fmt: wording error in fmt.Print docs? #19442 (closed)
- fmt: Inconsistent behaviour between fmt.Print() and fmt.Println() when supplying multiple arguments #41798 (closed)
- fmt.Print inserting spurious spaces #976 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: cherrymui
cc @robpike @martisch
Comment From: gopherbot
Change https://go.dev/cl/688877 mentions this issue: fmt: document space behavior of Append
Comment From: robpike
This should be addressed by updating all the docs to include the Append(|f|ln) functions in the discussions about Print, Sprint and so on. Just editing the doc comments for those functions is insufficient.
This should have been done when the functions were added.
Comment From: gopherbot
Change https://go.dev/cl/689355 mentions this issue: fmt: add missing space behavior docs and cross-references for Append