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

math/big: big.Int fmt.Formatter implementation should support %q #42022

Open
azdagron opened this issue Oct 16, 2020 · 2 comments
Open

math/big: big.Int fmt.Formatter implementation should support %q #42022

azdagron opened this issue Oct 16, 2020 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@azdagron
Copy link

I suggest a small quality-of-life improvement to the *big.Int type to support printing as a quoted string. *big.Int implements fmt.Formatter and (contrary to the documentation) supports the s verb, printing the integer in decimal format. This allows you to do:

fmt.Printf("My int: %s\n", big.NewInt(1))

However, it does not support q, which becomes surprising for people accustomed to the special formatting considerations for string formats wherein q is implicitly supported for types implementing fmt.Stringer. Instead you have to explicitly call the String() method:

fmt.Printf("My quoted int: %q\n", big.NewInt(1).String())

And if you forget?
https://play.golang.org/p/5YiBHW5djUM

I don't imagine there would be a big back-compat concern.

I'm happy to provide the code change if folks are on board.

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 16, 2020
@toothrot toothrot added this to the Backlog milestone Oct 16, 2020
@toothrot
Copy link
Contributor

/cc @griesemer

@AlexanderYastrebov
Copy link
Contributor

(contrary to the documentation) supports the s verb, printing the integer in decimal format.

Undocumented since 10 years https://codereview.appspot.com/4552056 and maybe it should not support it to avoid confusion.

Also it is interesting what output is expected for %q (single/double quotes, number base), e.g. plain int does not support %s at all and %q outputs a single-quoted character literal safely escaped with Go syntax. which might be not intuitive https://play.golang.org/p/o7pBzoVAFdV

I think the correct way for

fmt.Printf("My quoted int: %q\n", big.NewInt(1).String())

is

fmt.Printf("My quoted int: \"%d\"\n", big.NewInt(1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants