FAQ
By adding a String function to the type changes the behavior or the %x
formatting. Is this expected behavior, or a bug?
Without the String function, the %x operates as expected.

package main

import "fmt"

type X uint32
func (x X)String() string {return "???"}

var x =[]X{15}

func main() {
fmt.Printf("%#v\n",x) // expect []main.X{0xf}
fmt.Printf("%d\n",x[0]) // expect 15
fmt.Printf("%s\n",x[0]) // expect ???
fmt.Printf("%s\n",x[0].String()) // expect ???
fmt.Printf("0x%x\n",x[0]) // EXPECT 0x0f NOT 0x3f3f3f which is the bytes
for ???
fmt.Printf("0x%X\n",x[0]) // EXPECT 0x0f NOT 0x3F3F3F
fmt.Printf("0x%x\n",string(x[0])) // Not Sure, Don't expect 0x0f
}

Output:

[]main.X{0xf}
15
???
???
0x3f3f3f
0x3F3F3F
0x0f

--
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

  • Rob Pike at Feb 6, 2015 at 6:20 pm
    If fmt recognizes the type implements Stringer, it calls the method and
    treats the result as a string for any string-compatible format, which means
    %s %v %x %q but not %d.

    If you don't want the string, convert the argument to the type without it.

    fmt.Printf("0x%x\n", uint32(x[0]))

    -rob

    On Fri, Feb 6, 2015 at 8:50 AM, David Bailey wrote:

    By adding a String function to the type changes the behavior or the %x
    formatting. Is this expected behavior, or a bug?
    Without the String function, the %x operates as expected.

    package main

    import "fmt"

    type X uint32
    func (x X)String() string {return "???"}

    var x =[]X{15}

    func main() {
    fmt.Printf("%#v\n",x) // expect []main.X{0xf}
    fmt.Printf("%d\n",x[0]) // expect 15
    fmt.Printf("%s\n",x[0]) // expect ???
    fmt.Printf("%s\n",x[0].String()) // expect ???
    fmt.Printf("0x%x\n",x[0]) // EXPECT 0x0f NOT 0x3f3f3f which is the
    bytes for ???
    fmt.Printf("0x%X\n",x[0]) // EXPECT 0x0f NOT 0x3F3F3F
    fmt.Printf("0x%x\n",string(x[0])) // Not Sure, Don't expect 0x0f
    }

    Output:

    []main.X{0xf}
    15
    ???
    ???
    0x3f3f3f
    0x3F3F3F
    0x0f

    --
    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.
    --
    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
postedFeb 6, '15 at 5:44p
activeFeb 6, '15 at 6:20p
posts2
users2
websitegolang.org

2 users in discussion

David Bailey: 1 post Rob Pike: 1 post

People

Translate

site design / logo © 2021 Grokbase