FAQ
Printf renders the type of an array of a type derived from byte as a
byte slice. Is this intended?

http://play.golang.org/p/ROnPugHPX1

--

Search Discussions

  • Dave Cheney at Jan 22, 2013 at 4:03 am
    Interesting alias edge case.

    On Tue, Jan 22, 2013 at 2:08 PM, Dan Kortschak
    wrote:
    Printf renders the type of an array of a type derived from byte as a
    byte slice. Is this intended?

    http://play.golang.org/p/ROnPugHPX1

    --
    --
  • Dan Kortschak at Jan 22, 2013 at 4:32 am
    Issue submitted: golang.org/issue/4685
    On Tue, 2013-01-22 at 15:03 +1100, Dave Cheney wrote:
    Interesting alias edge case.

    On Tue, Jan 22, 2013 at 2:08 PM, Dan Kortschak
    wrote:
    Printf renders the type of an array of a type derived from byte as a
    byte slice. Is this intended?

    http://play.golang.org/p/ROnPugHPX1

    --

    --
  • Dan Kortschak at Jan 22, 2013 at 11:05 pm
    I thought I'd have a look into this and it seems that a fairly minor
    change fixes the fault.

    diff -r 073d912321cc src/pkg/fmt/print.go
    --- a/src/pkg/fmt/print.go Tue Jan 22 03:18:20 2013 +0800
    +++ b/src/pkg/fmt/print.go Wed Jan 23 09:34:15 2013 +1030
    @@ -939,7 +939,7 @@
    }
    case reflect.Array, reflect.Slice:
    // Byte slices are special.
    - if f.Type().Elem().Kind() == reflect.Uint8 {
    + if f.Type().Elem() == reflect.TypeOf(byte(0)) {
    // We know it's a slice of bytes, but we also know it does not have static type
    // []byte, or it would have been caught above. Therefore we cannot convert
    // it directly in the (slightly) obvious way: f.Interface().([]byte); it doesn't have

    Running the code at http://play.golang.org/p/mj_DExS5ag with this change
    gives:

    $ go run test.go
    []main.A []main.A{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
    []uint8 []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
    [10]main.A [10]main.A{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
    [10]main.B [10]main.B{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Which looks like what is probably intended. I'm concerned about the
    repeated calls to reflect.TypeOf() though. Is there an exported value
    that is like reflect.Uint8, but is a reflect.Type rather than a
    reflect.Kind? Or would a fmt pkg variable be worth declaring for this
    use? Or just deal with it?

    ./all.bash passes all tests with this change at tip.

    I'm happy to put together the tests and a CL for this if I can be given
    minor direction on this particular bit of the issue.

    thanks
    Dan
    On Tue, 2013-01-22 at 15:02 +1030, Dan Kortschak wrote:
    Issue submitted: golang.org/issue/4685

    --
  • Kamil Kisiel at Jan 22, 2013 at 4:08 am
    Seems like a bug to me. There's a special case for byte arrays and byte
    slices in the formatting code but there's no code path that can print the
    array dimensions.
    On Monday, January 21, 2013 7:08:07 PM UTC-8, kortschak wrote:

    Printf renders the type of an array of a type derived from byte as a
    byte slice. Is this intended?

    http://play.golang.org/p/ROnPugHPX1
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 22, '13 at 3:08a
activeJan 22, '13 at 11:05p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase