FAQ

The fmt package says:
For compound operands such as slices and structs, the format applies to
the elements of each operand, recursively

I'm witnessing this works for a slice of a custom type like "type rank int"
but not "type rank byte" as shown by this simple example.

package main

import "fmt"

type rank byte
var rankNames = []string{"Ace", "King", "Queen", "Jack"}
func (r rank) String() string { return rankNames[r] }

func main() {
ranks := []rank{0, 1, 2, 3}
fmt.Printf("Rank Slice: %v One Rank: %v\n", ranks, ranks[1])
}

OUTPUT IS:

$ go run test.go

Rank Slice: [0 1 2 3] One Rank: King


Note the individual rank got printed as King but the slice elements as
integers. If I change the type to "type rank int" then the slice prints
correctly using the names. It seems to work just fine for any integer type
(int8, uint16, etc) except not for byte or uint8.


Why the different behavior for byte vs the rest? Seems like a bug with the
format package to me. On a possibly related sidebar, I've notice the %b
verb to print in binary format works for int but not byte also. Seems like
byte is a second class citizen.


thanks,

bill


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jesse McNelis at Jul 8, 2014 at 1:44 am

    On Tue, Jul 8, 2014 at 11:18 AM, wrote:
    Why the different behavior for byte vs the rest? Seems like a bug with the
    format package to me. On a possibly related sidebar, I've notice the %b verb
    to print in binary format works for int but not byte also. Seems like byte
    is a second class citizen.
    I think it's a bug too.
    http://golang.org/src/pkg/fmt/print.go#L928




    --
    =====================
    http://jessta.id.au

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Jul 8, 2014 at 4:27 am
    Relevant history for that passage

    https://codereview.appspot.com/7205047
    On 08/07/2014, at 11:14 AM, "Jesse McNelis" wrote:
    On Tue, Jul 8, 2014 at 11:18 AM, wrote:
    Why the different behavior for byte vs the rest? Seems like a bug with the
    format package to me. On a possibly related sidebar, I've notice the %b verb
    to print in binary format works for int but not byte also. Seems like byte
    is a second class citizen.
    I think it's a bug too.
    http://golang.org/src/pkg/fmt/print.go#L928
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 8, '14 at 1:18a
activeJul 8, '14 at 4:27a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase