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.Sprintf does not handle percent character like fmt.printf does #39051

Closed
Marquichou opened this issue May 13, 2020 · 2 comments
Closed

fmt.Sprintf does not handle percent character like fmt.printf does #39051

Marquichou opened this issue May 13, 2020 · 2 comments

Comments

@Marquichou
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.13.5 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

fmt.Sprintf of a variable that contain the "%" character.
Here is a link to play.golang.org reproducing the issue: play.golang.org

What did you expect to see?

Let's say I have a string named str and set to "0%{?dist}".

Such as fmt.Printf, I expect to see something like:

str = "0%{?dist}"
sp := fmt.Sprintf("20: %s\n", str)
fmt.Printf(sp)


-> 20: 0%{?dist}

What did you see instead?

Sprintf does not seem to handle "%" character like Printf does.
Indeed, here is what Sprintf returns:

str = "0%{?dist}"
sp := fmt.Sprintf("20: %s\n", str)
fmt.Printf(sp)


-> 20: 0%!{(MISSING)?dist}

Errorf function has the same behavior with "%" character than Sprintf, but I guess this is because Errorf uses Sprintf.

Thank you !

@acln0
Copy link
Contributor

acln0 commented May 13, 2020

You are calling fmt.Printf, which tries to interpret the % in the string. The string is, in fact, what you expect it to be, but your code is going about printing it in the wrong way: https://play.golang.org/p/eoj7Bc5_MX1

@bradfitz
Copy link
Contributor

What @acln0 said. But also, you're calling fmt.Errorf with the first argument (the format) having a bogus format with percent signs. Use errors.New if you just want an error from a string (without a format).

@golang golang locked and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants