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: flag _ not exposed to custom Formatters #43851

Closed
josharian opened this issue Jan 22, 2021 · 4 comments
Closed

fmt: flag _ not exposed to custom Formatters #43851

josharian opened this issue Jan 22, 2021 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@josharian
Copy link
Contributor

josharian commented Jan 22, 2021

I attempted to write a custom Formatter that uses the _ flag.

Result: The custom Formatter doesn't receive the _ flag, and the format verb is printed (along with Format being called!).

Reproduction below and at https://play.golang.org/p/sPPOdzVnC-W.

In the Format "%_x": block in the output, note the _: false line as well as the stray "x" output.

package main

import (
	"fmt"
)

var flags = []int{'#', '+', '_', '0', ' '}

type T int

func (t T) Format(f fmt.State, verb rune) {
	for _, flag := range flags {
		fmt.Printf("\t%c: %v\n", flag, f.Flag(flag))
	}
}

func main() {
	var t T
	for _, flag := range flags {
		format := "%" + string(rune(flag)) + "x"
		fmt.Printf("Format %q:\n", format)
		fmt.Printf(format, t)
		fmt.Println()
	}
}

cc @robpike @martisch

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 22, 2021
@dmitshur dmitshur added this to the Backlog milestone Jan 22, 2021
@dmitshur

This comment has been minimized.

@robpike
Copy link
Contributor

robpike commented Jan 22, 2021

What is the _ flag?

@seankhliao
Copy link
Member

@josharian is this misreading the - flag (which is valid and fmtpasses) as _?

@josharian
Copy link
Contributor Author

OMG. Yes. Sheesh. Thank you. :)

But now I kinda wish fmt had an _ flag.

@golang golang locked and limited conversation to collaborators Jan 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants