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

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 2 | next ›
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