[ANN] Go-spew updated to Dump byte arrays and slice like hexdump -C

270 views
Skip to first unread message

Dave Collins

unread,
Mar 9, 2013, 12:11:11 AM3/9/13
to golan...@googlegroups.com
Hello gophers,

I've just updated go-spew (https://github.com/davecgh/go-spew) to dump byte
arrays and slices like the hexdump -C command does.  While doing some protocol
work, I found myself dumping byte slices frequently.  They really didn't print
all that nicely, so this is the result.

Assuming the following code:

type foo struct {
    n int
    b []byte
}
b := []byte{
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
    0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
    0x21, 0x22, 0x23, 0x34, 0x48, 0x65, 0x6c, 0x6c,
    0x6f, 0x20, 0x47, 0x6f, 0x70, 0x68, 0x65, 0x72,
    0x73, 0x21,
}
f := foo{123, b}


The spew.Dump(f) output is as follows:

(main.foo) {
 n: (int) 123,
 b: ([]uint8) {
  00000000  11 12 13 14 15 16 17 18  19 1a 1b 1c 1d 1e 1f 20  |............... |
  00000010  21 22 23 34 48 65 6c 6c  6f 20 47 6f 70 68 65 72  |!"#4Hello Gopher|
  00000020  73 21                                             |s!|
 }
}

Dan Kortschak

unread,
Mar 9, 2013, 12:35:55 AM3/9/13
to Dave Collins, golan...@googlegroups.com
Very nice. Thanks for your work.
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Damian Gryski

unread,
Mar 9, 2013, 2:03:49 AM3/9/13
to golan...@googlegroups.com
I've also been doing some protocol work, but mine was in C (server) and Perl (client).  As a result, I wanted something I could inject between them to see what was going across the wire rather than havinv to fall back to wireshark.

The result is https://github.com/dgryski/dhd (mostly  undocumented, but that's what you get with personal utilities sometimes :)

The short version is:

terminal1$ nc -l 10000 # listen on port 10000
terminal2$ dhd -p 10001:localhost:10000   # proxy local port 10001 -> localhost:10000
terminal3$ nc localhost 10001   # connect to local port 10001

And now you'll get output like:
2013/03/09 08:00:26 tcp server starting
>00000000:  68 65 6c 6c  6f 2c 20 77  6f 72 6c 64  0a             |hello,.world.|
<00000000:  68 65 6c 6c  6f 20 62 61  63 6b 0a                    |hello.back.|

I have a couple of small TODOs, like adding connection info and timestamps to the output, but this has been sufficient for me to hunt down some pretty ugly bugs on a couple of occasions.

Damian

Dan Kortschak

unread,
Mar 9, 2013, 2:19:51 AM3/9/13
to Dave Collins, golan...@googlegroups.com
An idea that sort of (tenuously) follows from this is the capacity to print Go syntax dumps with reasonable line lengths (obviously excluding circular structures). This would be a boon when including, for example, application data that's generated and included in the compilation unit.

minux

unread,
Mar 9, 2013, 4:03:33 PM3/9/13
to Dave Collins, golan...@googlegroups.com
On Sat, Mar 9, 2013 at 1:11 PM, Dave Collins <da...@davec.name> wrote:
> I've just updated go-spew (https://github.com/davecgh/go-spew) to dump byte
> arrays and slices like the hexdump -C command does. While doing some
> protocol
> work, I found myself dumping byte slices frequently. They really didn't
> print
> all that nicely, so this is the result.
Thank you for this great debugging aid.
Reply all
Reply to author
Forward
0 new messages