Skip to content
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: more doc needed for Formatter #42476

dolmen opened this issue Nov 10, 2020 · 2 comments

fmt: more doc needed for Formatter #42476

dolmen opened this issue Nov 10, 2020 · 2 comments


Copy link

@dolmen dolmen commented Nov 10, 2020

Current (go1.15, tip) documentation of interface fmt.Formatter:

Formatter is implemented by any value that has a Format method. The implementation controls how State and rune are interpreted, and may call Sprint(f) or Fprint(f) etc. to generate its output.

This is insufficient to tell how one must implement this interface:

  • which verbs should be handled for each type?
  • how to handle unhandled verbs?
  • how to forward unhandled verbs to Printf?
  • how to handle unhandled width?
  • how to handle unhandled precision?
  • lack of example
Copy link

@martisch martisch commented Nov 10, 2020

There are no constraints how to implement formatters apart from the function signature. Its up to the developer to decide what should be handled how for each type. Which verbs produce output and which dont. Which verbs are implemented directly and which are formatted using other functions.

An example could be added. Contributions welcome.

Copy link

@dolmen dolmen commented Nov 12, 2020

Yes, there are no constraints. But guidelines or best practices would still be helpful.

Also the stdlib has very few implementation of that interface (grep 'Format(fmt\.State,' $(go env GOROOT)/api/*.txt lists only 2 in package math/big) so reference code is limited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.