Description
Add print functions to fmt that support formatting and append a new line, e.g. Printlnf
, Fprintlnf
, etc.
The fmt
package has these printing functions today:
Fprint
Fprintf
Fprintln
Print
Printf
Println
Sprint
Sprintf
Sprintln
The ln
functions are convenient when printing lines to stderr and stdout. The f
functions are required for formatting but have no ln
equivalent.
It is a pretty common occurrence for me to write or edit code that uses Println
and Fprintln
, then to have a need to add a formatted parameter to that output. Without fail I rename the function to Printf
and forget to include a \n
at the end.
If f
variants are added for all the ln
functions, there would be functions Printlnf
, Fprintlnf
, etc.
The intended uses of the existing functions would become:
Print
- Print, no new line appended, no formatting.Printf
- Print, with formatting.Println
- Print, new line appended.Printlnf
- Print, with formatting, new line appended.
A prototype implementation is available at https://4d63.com/fmt.
Example:
func Printlnf(format string, a ...interface{}) (n int, err error)
func Fprintlnf(format string, a ...interface{}) (n int, err error)
code := 400
message := "error message"
fmt.Fprintlnf(os.Stderr, "error: code %d, %s", code, message)
fmt.Fprintln(os.Stderr, "resending request")
// Output:
// error: code 400, error message
// resending request
It may also make sense for consistency to add the same variation of functions for each scan function in the fmt package.
Proposal template:
-
Would you consider yourself a novice, intermediate, or experienced Go programmer?
Experienced -
What other languages do you have experience with?
Java, Ruby, C#, C, JavaScript -
Would this change make Go easier or harder to learn, and why?
A little easier. -
Has this idea, or one like it, been proposed before?
Yes, We should have fmt.Printfln series of functions in package #31214.- If so, how does this proposal differ?
It's identical. The author abandoned their proposal.
- If so, how does this proposal differ?
-
Who does this proposal help, and why?
Anyone printing lines. -
What is the proposed change?
See above.-
Please describe as precisely as possible the change to the language.
See above. -
What would change in the language spec?
Nothing. -
Please also describe the change informally, as in a class teaching Go.
See above.
-
-
Is this change backward compatible?
Yes- Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit.
-
Show example code before and after the change.
See above. -
What is the cost of this proposal? (Every language change has a cost).
- How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
None. - What is the compile time cost?
Little. - What is the run time cost?
None.
- How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
-
Can you describe a possible implementation?
See above.- Do you have a prototype? (This is not required.)
Yes. See https://4d63.com/fmt.
- Do you have a prototype? (This is not required.)
-
How would the language spec change?
It wouldn't. -
Orthogonality: how does this change interact or overlap with existing features?
It is modeled after existing print functions in fmt package. -
Is the goal of this change a performance improvement?
No.- If so, what quantifiable improvement should we expect?
- How would we measure it?
-
Does this affect error handling?
No.- If so, how does this differ from previous error handling proposals?
-
Is this about generics?
No.- If so, how does this differ from the the current design draft and the previous generics proposals?