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

Hex formatting (%x) returns a hexidecimal memory address if a custom Integer type implements the Stringer interface #3974

Closed
nathany opened this issue Aug 19, 2012 · 2 comments

Comments

@nathany
Copy link
Contributor

nathany commented Aug 19, 2012

What steps will reproduce the problem?

http://play.golang.org/p/2RJlF30QTC

1. Define a custom integer type
2. Implement a String() method on the type
3. Use the custom type with the %x or %X formatter

What is the expected output?

I expected %x to print the hexidecimal value, in this example "ab".

What do you see instead?

The hexidecimal memory address, as though I had passed &x.

Which compiler are you using (5g, 6g, 8g, gccgo)?

6g

Which operating system are you using?

darwin

Which version are you using?  (run 'go version')

1.0.2, also reproduced on tip ("weekly.2012-03-27 +ac1b735e8753")

Please provide any additional information below.

As per the example:

* the decimal format is still correct
* other methods on Integer don't cause an issue
* the same issue occurs whether using Itoa vs. Sprintf in String()
* it's not possible to dereference *x to get the hex value

On a related note, if String() returns an empty string, the hex value and hexAddress
printed are both empty/nothing.
@remyoudompheng
Copy link
Contributor

Comment 1:

What you are seeing is not a memory address. You are seeing the hexadecimal dump of the
bytes that compose the string "171" (the result of x.String())
This is working as intended according to the fmt package documentation:
 "If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules also apply:
1. If an operand implements the error interface, the Error method will be used to
convert the object to a string, which will then be formatted as required by the verb (if
any).
2. If an operand implements method String() string, that method will be used to convert
the object to a string, which will then be formatted as required by the verb (if any)."
This is why %d gives you "171". Printing x and &x will give the same result because both
of them have the String() method. If you want to see "AB", use
fmt.Printf("%x", int(x))

Status changed to WorkingAsIntended.

@nathany
Copy link
Contributor Author

nathany commented Aug 19, 2012

Comment 2:

Hi Rémy,
Thanks for the explanation. Sorry for not reading the documentation more thoroughly
before reporting this.
Nathan.

@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
Development

No branches or pull requests

3 participants