-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: fmt: improve ergonomics of fmt.Print-like functions #57816
Comments
The |
To me it looks ugly to call fmt.Sprintf from within fmt.Print (or fmt.Sprint). There's precedent for this in the standard library, strconv.FormatInt and strconv.FormatFloat also duplicate the functionality of |
I'm not clear on why |
I don't think it's a good idea for |
In this case, none, the primary reason is to allow access through fmt.Print to formatting values like %#v, %+v and %#+v do, which at the moment isn't possible without using fmt.Printf. The secondary reason would be to disable automatic insertion of spaces.
In those cases one can call the Error method directly. |
To be clear, I think the default should be |
I do broadly agree that error wrapping is the case that should be explicit, but I wonder if choosing to call Perhaps it would be harder to accidentally misuse if renamed and constrained a little more tightly:
The second of these is tricky in practice because there's no way to write a variadic signature whose last argument is more constrained than those before it, and so it would need to be declared as I also considered As someone who admitted in the other issue that I don't know why I've been using All of those except Therefore I feel ambivalent about this proposal; I like the idea of it but the details don't feel quite right yet. |
I disagree that fmt.Printf should be discouraged over fmt.Print. The helpers suggested here provide essentially the non-f form of Errorf, an implementation of %T (which can also just be reflect.TypeOf), and an implementation of %v. It's unclear why code would call these much longer names instead of writing a printf format. The semantics of Error with respect to Unwrap are also problematic: we used %w as an explicit signal that the error should unwrap. The default %v does not. So Error, if it did exist, should not automatically unwrap to any errors passed in. |
This proposal has been added to the active column of the proposals project |
I wrote this because it seemed like there would be interest based on the discussion about string interpolation but it doesn't look like anyone is interested in a simpler syntax for that, besides string interpolation itself. I'd say everything should be considered retracted besides fmt.Error, which makes sense to me for symmetry. |
It doesn't seem like there is enough support for fmt.Error (without f), and we probably don't also want fmt.Errorln. |
Based on the discussion above, this proposal seems like a likely decline. |
Fair enough. |
I'm not a fan of this proposal itself, but this was a great discussion and a cool methodology to start it off! Thanks @aarzilli |
This proposal has been declined as retracted. |
Proposals #57616, #50554 and #34174 all make a good argument for why functions in the fmt.Print family should be preferred to functions in the fmt.Printf family. However the fmt.Print family is rarely used in practice, so much that people even forget it exists 1 2.
The reasons for fmt.Print neglect are unknown but I propose that the reason is that the fmt.Print family does not have a few frequently used features that can only be accessed through fmt.Printf. I propose we add the following to the
fmt
package:Furthermore I propose that we change the documentation of Sprint to say the following:
Methodology
I downloaded the 10 most starred Go projects on github according to 3 excluding Go itself and awesome-go (which is a collection of links). I then used grep to find uses of Print and Printf like functions. I found the following uses:
I then parsed the first argument (when it was possible) and identified all formatting directives, including the character immediately following each of them. These were all the formatting directives used at least 100 times:
Cons
The text was updated successfully, but these errors were encountered: