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: clarify when %#q does not use backquotes #4858

Closed
gopherbot opened this issue Feb 20, 2013 · 8 comments
Closed

fmt: clarify when %#q does not use backquotes #4858

gopherbot opened this issue Feb 20, 2013 · 8 comments
Milestone

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Feb 20, 2013

by proebsting:

Why does fmt.Printf("%#q", "a\nb") fail to use backquotes rather
than falling back on double quotes?  There's nothing special about newlines in
backquoted strings, is there?

Thanks.
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Feb 20, 2013

Comment 1:

I think that would be surprising and would probably break the Go 1 API promise, in a
way. %q is often used in tests to show failures, showing empty strings or strings with
accidental binary data.  I wouldn't like to see `` vs "" flipping back and forth,
depending on other binary data or chars needs to be escaped, or \r, etc.  Having one way
seems best, even if not ideal.

Status changed to WorkingAsIntended.

@minux
Copy link
Member

@minux minux commented Feb 20, 2013

Comment 2:

fmt just asks strconv.CanBackquote wether a given string can be safely
represented if it's enclosed in backquotes.
I don't know why "\n" is not allowed, perhaps CanBackquote just want
to make sure the string contain only one line if printed out?
@rsc
Copy link
Contributor

@rsc rsc commented Feb 20, 2013

Comment 3:

Yes, the goal is to avoid multiline output from %#q.
@gopherbot
Copy link
Author

@gopherbot gopherbot commented Feb 20, 2013

Comment 4 by proebsting:

I would argue the documentation is misleading then.  It says the following:
#   alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
    0X for hex (%#X); suppress 0x for %p (%#p);
    print a raw (backquoted) string if possible for %q (%#q);
Clearly, it is possible to print the string with backquotes.
@rsc
Copy link
Contributor

@rsc rsc commented Feb 20, 2013

Comment 5:

Labels changed: added priority-later, go1.1, removed priority-triage, go1.1maybe.

Status changed to Accepted.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Feb 20, 2013

Comment 6:

Wow, I stand corrected. I had never seen that. (# on %q)
@minux
Copy link
Member

@minux minux commented Feb 20, 2013

Comment 7:

https://golang.org/cl/7387044

Status changed to Started.

@minux
Copy link
Member

@minux minux commented Feb 25, 2013

Comment 8:

This issue was closed by revision 4692711.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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