github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/cmd/structlayout/README.md (about) 1 # structlayout 2 3 The _structlayout_ utility prints the layout of a struct – that is the 4 byte offset and size of each field, respecting alignment/padding. 5 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. 9 10 A utility called _structlayout-pretty_ takes this JSON and prints an 11 ASCII graphic representing the memory layout. 12 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_. 18 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 22 23 ``` 24 go get github.com/ajstarks/svgo/structlayout-svg 25 ``` 26 27 ## Installation 28 29 See [the main README](https://github.com/dominikh/go-tools#installation) for installation instructions. 30 31 ## Examples 32 33 ``` 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) 42 ``` 43 44 ``` 45 $ structlayout -json bufio Reader | jq . 46 [ 47 { 48 "name": "Reader.buf", 49 "type": "[]byte", 50 "start": 0, 51 "end": 24, 52 "size": 24, 53 "is_padding": false 54 }, 55 { 56 "name": "Reader.rd", 57 "type": "io.Reader", 58 "start": 24, 59 "end": 40, 60 "size": 16, 61 "is_padding": false 62 }, 63 { 64 "name": "Reader.r", 65 "type": "int", 66 "start": 40, 67 "end": 48, 68 "size": 8, 69 "is_padding": false 70 }, 71 ... 72 ``` 73 74 ``` 75 $ structlayout -json bufio Reader | structlayout-pretty 76 +--------+ 77 0 | | <- Reader.buf []byte 78 +--------+ 79 -........- 80 +--------+ 81 23 | | 82 +--------+ 83 24 | | <- Reader.rd io.Reader 84 +--------+ 85 -........- 86 +--------+ 87 39 | | 88 +--------+ 89 40 | | <- Reader.r int 90 +--------+ 91 -........- 92 +--------+ 93 47 | | 94 +--------+ 95 48 | | <- Reader.w int 96 +--------+ 97 -........- 98 +--------+ 99 55 | | 100 +--------+ 101 56 | | <- Reader.err error 102 +--------+ 103 -........- 104 +--------+ 105 71 | | 106 +--------+ 107 72 | | <- Reader.lastByte int 108 +--------+ 109 -........- 110 +--------+ 111 79 | | 112 +--------+ 113 80 | | <- Reader.lastRuneSize int 114 +--------+ 115 -........- 116 +--------+ 117 87 | | 118 +--------+ 119 ``` 120 121 ``` 122 $ structlayout -json bytes Buffer | structlayout-svg -t "bytes.Buffer" > /tmp/struct.svg 123 ``` 124 125 ![memory layout of bytes.Buffer](/images/screenshots/struct.png)