github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/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 ``` 30 go get github.com/golangci/go-tools/cmd/structlayout 31 go get github.com/golangci/go-tools/cmd/structlayout-pretty 32 go get github.com/golangci/go-tools/cmd/structlayout-optimize 33 ``` 34 35 ## Examples 36 37 ``` 38 $ structlayout bufio Reader 39 Reader.buf []byte: 0-24 (24 bytes) 40 Reader.rd io.Reader: 24-40 (16 bytes) 41 Reader.r int: 40-48 (8 bytes) 42 Reader.w int: 48-56 (8 bytes) 43 Reader.err error: 56-72 (16 bytes) 44 Reader.lastByte int: 72-80 (8 bytes) 45 Reader.lastRuneSize int: 80-88 (8 bytes) 46 ``` 47 48 ``` 49 $ structlayout -json bufio Reader | jq . 50 [ 51 { 52 "name": "Reader.buf", 53 "type": "[]byte", 54 "start": 0, 55 "end": 24, 56 "size": 24, 57 "is_padding": false 58 }, 59 { 60 "name": "Reader.rd", 61 "type": "io.Reader", 62 "start": 24, 63 "end": 40, 64 "size": 16, 65 "is_padding": false 66 }, 67 { 68 "name": "Reader.r", 69 "type": "int", 70 "start": 40, 71 "end": 48, 72 "size": 8, 73 "is_padding": false 74 }, 75 ... 76 ``` 77 78 ``` 79 $ structlayout -json bufio Reader | structlayout-pretty 80 +--------+ 81 0 | | <- Reader.buf []byte 82 +--------+ 83 -........- 84 +--------+ 85 23 | | 86 +--------+ 87 24 | | <- Reader.rd io.Reader 88 +--------+ 89 -........- 90 +--------+ 91 39 | | 92 +--------+ 93 40 | | <- Reader.r int 94 +--------+ 95 -........- 96 +--------+ 97 47 | | 98 +--------+ 99 48 | | <- Reader.w int 100 +--------+ 101 -........- 102 +--------+ 103 55 | | 104 +--------+ 105 56 | | <- Reader.err error 106 +--------+ 107 -........- 108 +--------+ 109 71 | | 110 +--------+ 111 72 | | <- Reader.lastByte int 112 +--------+ 113 -........- 114 +--------+ 115 79 | | 116 +--------+ 117 80 | | <- Reader.lastRuneSize int 118 +--------+ 119 -........- 120 +--------+ 121 87 | | 122 +--------+ 123 ``` 124 125 ``` 126 $ structlayout -json bytes Buffer | structlayout-svg -t "bytes.Buffer" > /tmp/struct.svg 127 ``` 128 129 ![memory layout of bytes.Buffer](/images/screenshots/struct.png)