-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: fmt: add Printlnf, Fprintlnf, etc #46190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
My intuition is that you win very little by adding more variants, and this will be very confusing to people trying to read code - particularly those new to Go. It's not that hard to write |
This is what I thought would happen for me, but it hasn't. I still forget to add
Could you expand on why this will be very confusing? |
Mistakes can happen. I imagine you're just as likely to forget to add
More options to choose from, mainly. Especially since all these func names vary only by one or two characters. We already have quite a lot of variants, and they exist for a good reason. More variants when it's just a very minor convenience doesn't seem worth it to me. |
I don't like this idea. The https://play.golang.org/p/aLlMHWQbCkC fmt.Sprint(" a ", " b ") == " a b " // two spaces
fmt.Sprintln(" a ", " b ") == " a b \n" // three spaces What does the |
It might make it clearer if the function was named @adonovan Would that make the fln variant clearer?
The |
No. The problem is that behavior of |
#46190 (comment) is the major reason not to do this. Println is not Print+\n. It changes other things that make no sense to change in Printf. |
@rsc @adonovan If we think of I'm somewhat in favor of this proposal. Like @leighmcculloch, I often forget |
This proposal has been added to the active column of the proposals project |
The main problem this solves is addressing the mistake some of are repeatedly making with missing |
I too sometimes forget to put There are clear reasons in this thread why these proposed functions would be confusing, and the design decisions around I don't think the inevitable confusion could ever be worth it for a problem as trivial as forgetting a newline. |
Did you mean that's not what Comparing the definitions of
https://golang.org/pkg/fmt/#Printf
|
|
Based on the discussion above, this proposal seems like a likely decline. |
It is actually hard to write when you‘re on a German keyboard. The backslash is a pain and it would be great not having to use it all the time for simple log statements. |
This feels like one of those real problems that can be trivially solved within each project, versus making it an explicit feature of the language and needing to support the API in perpetuity (i.e., until Go 2). If you run into this problem often, you could copy this helper function into all the packages you use that need it. It seems like it would be unlikely to change, and so it feels like a great candidate for "A little copying is better than a little dependency": func printf(format string, a ...interface{}) (n int, err error) {
return fmt.Printf(format+"\n", a...)
} |
This argument doesn't make sense to me. We already have |
No change in consensus, so declined. |
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. Thef
functions are required for formatting but have noln
equivalent.It is a pretty common occurrence for me to write or edit code that uses
Println
andFprintln
, then to have a need to add a formatted parameter to that output. Without fail I rename the function toPrintf
and forget to include a\n
at the end.If
f
variants are added for all theln
functions, there would be functionsPrintlnf
,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:
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.
It's identical. The author abandoned their proposal.
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
Show example code before and after the change.
See above.
What is the cost of this proposal? (Every language change has a cost).
None.
Little.
None.
Can you describe a possible implementation?
See above.
Yes. See https://4d63.com/fmt.
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.
Does this affect error handling?
No.
Is this about generics?
No.
The text was updated successfully, but these errors were encountered: