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
fmt: add FormatString(State) string #51668
Comments
To clarify, I don't think we should add the (In retrospect I think it was a mistake to make |
Technically we can't add a method to the |
CC @robpike |
What would that let us do, though? Where I am imagining this would be most useful is in third party code that is inside of a func (b myType) Format(w fmt.State, verb rune) {
// here
} The general shape of that function, I would imagine, is something like the other proposal:
where ??? is, in theory, the recovered passed in format string. If edit: ahhh... if we added String() you could do |
I dont think this suggestion would work the way you are expecting. If I take your example: func (b myType) Format(w fmt.State, verb rune) {
// here
} and then expand upon it: func (b myType) Format(w fmt.State, verb rune) {
fmt.Fprintf(w, w.String(), b)
} you get a stack overflow. Can you post an example of how you would use |
@ianlancetaylor presumably it'd look like:
(that would mask any myType.String method though, and |
That said, I believe #51195 (comment) is cleaner.
|
I disagree with this. If So really, you're solving one problem, and creating two more. |
I'm not sure we should be adding invisible methods here. The solution proposed in #51195 is completely visible - it doesn't need people to know things that aren't in the type system. A new method you type-assert would be different. |
This proposal has been added to the active column of the proposals project |
One possibility is to have a top-level function from fmt.State to string, like
|
Change https://go.dev/cl/400875 mentions this issue: |
@knz implemented https://github.com/knz/go-fmtfwd with some difficulty to work around this very issue. |
@robpike has posted a CL at https://go-review.googlesource.com/c/go/+/400875. The API is almost what I wrote earlier, adding 'verb rune' to the argument list:
This provides access to what was requested, just without a new State method. Does anyone object to this API? |
Actually it's not quite the same. The actual change is
as State does not contain the verb. The verb is always at the end, so the caller could add it, but doing it here can remove an allocation. |
Would it be possible to preserve a slice that refers to the portion in the original format string in the state, and return that instead of constructing a new string?
…--
Verstuurd vanaf mijn Android apparaat met K-9 Mail. Excuseer mijn beknoptheid.
|
No, because State is an interface. |
Updated comment to record 'verb rune' in API. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. |
I'm currently working with a custom fmt.Formatter implementation. Similar to the request in #51195, I would like to implement some methods and then "fall back" to the default
fmt
implementation for unimplemented verbs.It's difficult to do this because State does not allow you to reconstruct the original format string, unless you enumerate all of the possible characters in a format string and call
Flag(char)
on each one. This is lengthy and error prone.I would like to formally propose what @bcmills suggested in #25150, which is to add a String() api to
fmt.State
.I doubt that there are many implementations of the API, which would limit the amount of breakage from adding a new method.
In the standard library, there is currently only one implementation of
fmt.State
- in thepp
struct.If someone can give me pointers on how to do a search across all of Github, I would be happy to check whether there are in-the-wild implementations of fmt.State. I'd also appreciate if someone could do the same inside of Google.
Thanks to Bryan Mills, Github user seebs and Eric Lagergren for initial suggestions and discussion.
The text was updated successfully, but these errors were encountered: