You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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))
The text was updated successfully, but these errors were encountered: