Hi folks,

I noticed that the following format string handles the width in an
unexpected way (the "gosnip" program just runs snippets of Go code):

$ gosnip 'fmt.Printf("%#05x\n", 45)'
0x0002d

I would have expected the width (5) to include the "0x", but it's prepended
to the string. In contrast, when you do the same thing without "0" prefix,
the width of 5 includes the "0x":

$ gosnip 'fmt.Printf("%#5x\n", 45)'
 0x2d

I realize Go's fmt is not intended to mimic C exactly, but for reference, C
printf outputs "0x02d" for the first case, which is what I would have
expected.

The Go docs are somewhat unclear. For the '#' flag it says "alternate
format: add leading 0x for hex (%#x)". Which kind of implies the behavior
in the first example -- that it just adds a leading "0x" to the string.
However, as per the second example, if you leave off the "0" width prefix,
it includes the "0x" in the width. So it seems inconsistent.

For reference, there is one test of this behavior, not in the width tests
section, but indirectly in the reflect.Value tests:

{"%#04x", reflect.ValueOf(256), "0x0100"},

Any ideas why the width doesn't include the 0x in the first example, but
does in the second? Is this expected?

-Ben

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to