3 The _structlayout_ utility prints the layout of a struct – that is the
4 byte offset and size of each field, respecting alignment/padding.
6 The information is printed in human-readable form by default, but can
7 be emitted as JSON with the `-json` flag. This makes it easy to
8 consume this information in other tools.
10 A utility called _structlayout-pretty_ takes this JSON and prints an
11 ASCII graphic representing the memory layout.
13 _structlayout-optimize_ is another tool. Inspired by
14 [maligned](https://github.com/mdempsky/maligned), it reads
15 _structlayout_ JSON on stdin and reorders fields to minimize the
16 amount of padding. The tool can itself emit JSON and feed into e.g.
17 _structlayout-pretty_.
19 _structlayout-svg_ is a third-party tool that, similarly to
20 _structlayout-pretty_, visualises struct layouts. It does so by
21 generating a fancy-looking SVG graphic. You can install it via
24 go get github.com/ajstarks/svgo/structlayout-svg
29 See [the main README](https://github.com/dominikh/go-tools#installation) for installation instructions.
34 $ structlayout bufio Reader
35 Reader.buf []byte: 0-24 (24 bytes)
36 Reader.rd io.Reader: 24-40 (16 bytes)
37 Reader.r int: 40-48 (8 bytes)
38 Reader.w int: 48-56 (8 bytes)
39 Reader.err error: 56-72 (16 bytes)
40 Reader.lastByte int: 72-80 (8 bytes)
41 Reader.lastRuneSize int: 80-88 (8 bytes)
45 $ structlayout -json bufio Reader | jq .
75 $ structlayout -json bufio Reader | structlayout-pretty
77 0 | | <- Reader.buf []byte
83 24 | | <- Reader.rd io.Reader
89 40 | | <- Reader.r int
95 48 | | <- Reader.w int
101 56 | | <- Reader.err error
107 72 | | <- Reader.lastByte int
113 80 | | <- Reader.lastRuneSize int
122 $ structlayout -json bytes Buffer | structlayout-svg -t "bytes.Buffer" > /tmp/struct.svg
125 ![memory layout of bytes.Buffer](/images/screenshots/struct.png)