Skip to content

fmt.Printf("%p", x) does not work if x has a String method. #1024

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
rogpeppe opened this issue Aug 12, 2010 · 3 comments
Closed

fmt.Printf("%p", x) does not work if x has a String method. #1024

rogpeppe opened this issue Aug 12, 2010 · 3 comments

Comments

@rogpeppe
Copy link
Contributor

the following code prints "%p(string=hello)".

this is a direct consequence of the recent changes to
fmt to switch on type first, but it's not helpful behaviour
and it's not easy to work around without importing
reflect and rewriting fmtUintptrGetter.

might i suggest that fmt.printField only check for Stringer/GoStringer
when the verb can actually process a string.

package main
import "fmt"
type T struct{}
func main() { fmt.Printf("%p\n", &T{}) }
func (x *T) String() string { return "hello" }
@rsc
Copy link
Contributor

rsc commented Aug 12, 2010

Comment 1:

same is true of %d.  it was a conscious choice to clean things up.
i suspect this is working as intended; leaving for rob.

Owner changed to r...@golang.org.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Aug 13, 2010

Comment 2:

Here's a related and to my mind more serious inconsistency.
Println of one argument is supposed to be like Printf with "%v\n" but
this program prints two different lines:
package main
import "fmt"
type T int
func (t T) Format(f fmt.State, c int) {
    fmt.Fprintf(f, "-formatted-")
}
func main() {
    fmt.Printf("%v\n", T(1))
    fmt.Println(T(1))
}

@robpike
Copy link
Contributor

robpike commented Aug 13, 2010

Comment 3:

This issue was closed by revision b21611b.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
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