Go Programming Tips

Add 0x prefix when printing hex numbers

Use %#x placeholder.

1
2
3
4
5
func TestPrintHex()  {
var i int = 0x123456
fmt.Printf("%x\n", i)
fmt.Printf("%#x\n", i)
}
1
2
123456
0x123456
1
2
3
fmt.Printf("%T\n", i)
fmt.Println(reflect.Typeof(i))
fmt.Prinft("%s\n", reflect.Typeof(i).String())
Read more