this is interesting. it seem slightly inconsistent, and the documentation has nothing to say about ...
14 years, 5 months ago
(2010-10-04 10:43:31 UTC)
#4
this is interesting.
it seem slightly inconsistent, and the documentation has nothing
to say about behaviour with respect to slices other
than slices of byte, AFAICS.
fmt.Sprintf("%d", []int{10, 11, 12}) -> "[10 11 12]"
fmt.Sprintf("%x", []int{10, 11, 12}) -> "[a b c]"
fmt.Sprintf("%d", []byte{10, 11, 12}) -> "[10 11 12]"
fmt.Sprintf("%x", []byte{10, 11, 12}) -> "0a0b0c"
at the least, i think it should be documented that %d (and %o and... ?)
work on slices; and if Printf works, why not Scanf too?
i had previously assumed that for anything other than the basic
types, it was necessary to use %v. but
fmt.Printf("%x\n", struct{a, b int; c string}{10,11,"foo"})
works too. what other interesting behaviour is lurking there?
On 4 October 2010 10:57, <r@golang.org> wrote:
> *** Submitted as
> http://code.google.com/p/go/source/detail?r=70ddef42f94e ***
>
> fmt: allow %d on []byte
> Fixes issue 1159.
>
> R=rsc, adg
> CC=golang-dev
> http://codereview.appspot.com/2305043
>
>
> http://codereview.appspot.com/2305043/
>
you're right that things aren't as well documented as they could be. (is printf ever ...
14 years, 5 months ago
(2010-10-04 11:56:25 UTC)
#5
you're right that things aren't as well documented as they could be. (is printf
ever well documented?)
but here's how to think about it. after a recent rewrite, the verbs are
secondary. that is, printf prints according to the type of the argument first.
the verb can color the result, as you see with the struct example. for
composite objects, the effect is mostly to apply the verb to its elements.
there's probably a good way to explain this, and i'm sure there are cases one
can construct where you don't get what you expect. %d on []byte was one; %o
etc. on []byte won't work at the moment, but that's a lack of the implementation
rather than the approach.
-rob
On Oct 4, 2010, at 12:43 PM, roger peppe wrote:
> this is interesting.
>
> it seem slightly inconsistent, and the documentation has nothing
> to say about behaviour with respect to slices other
> than slices of byte, AFAICS.
>
> fmt.Sprintf("%d", []int{10, 11, 12}) -> "[10 11 12]"
> fmt.Sprintf("%x", []int{10, 11, 12}) -> "[a b c]"
> fmt.Sprintf("%d", []byte{10, 11, 12}) -> "[10 11 12]"
> fmt.Sprintf("%x", []byte{10, 11, 12}) -> "0a0b0c"
>
> at the least, i think it should be documented that %d (and %o and... ?)
> work on slices; and if Printf works, why not Scanf too?
>
> i had previously assumed that for anything other than the basic
> types, it was necessary to use %v. but
>
> fmt.Printf("%x\n", struct{a, b int; c string}{10,11,"foo"})
>
> works too. what other interesting behaviour is lurking there?
>
> On 4 October 2010 10:57, <r@golang.org> wrote:
>> *** Submitted as
>> http://code.google.com/p/go/source/detail?r=70ddef42f94e ***
>>
>> fmt: allow %d on []byte
>> Fixes issue 1159.
>>
>> R=rsc, adg
>> CC=golang-dev
>> http://codereview.appspot.com/2305043
>>
>>
>> http://codereview.appspot.com/2305043/
>>
Issue 2305043: code review 2305043: fmt: allow %d on []byte
(Closed)
Created 14 years, 5 months ago by r
Modified 14 years, 5 months ago
Reviewers: rog, r2
Base URL:
Comments: 0