Skip to content

fmt: explain how Formatter interface affects verbs and flags #39860

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fmt/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@
concrete value that it holds, and printing continues with the next rule.

2. If an operand implements the Formatter interface, it will
be invoked. Formatter provides fine control of formatting.
be invoked. In this case the interpretation of verbs and flags is
controlled by that implementation.

3. If the %v verb is used with the # flag (%#v) and the operand
implements the GoStringer interface, that will be invoked.
Expand Down
8 changes: 4 additions & 4 deletions src/fmt/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ type State interface {
Flag(c int) bool
}

// Formatter is the interface implemented by values with a custom formatter.
// The implementation of Format may call Sprint(f) or Fprint(f) etc.
// to generate its output.
// 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.
type Formatter interface {
Format(f State, c rune)
Format(f State, verb rune)
}

// Stringer is implemented by any value that has a String method,
Expand Down