Skip to content
IsaacShelton edited this page Mar 21, 2022 · 1 revision

packed

By default, structures contain padding in order to improve access performance.

The packed keyword can be used to disable struct padding.

packed struct MyFileHeader (magic ushort, version uint, offset1, offset2 usize)

This is good for values that will be stored on disk or sent over the network.

Example

import basics

// Size difference between packed versus unpacked.
// This is usually only apparent when field types
// are non-uniform.
       struct One (a int, b long, c int)
packed struct Two (a int, b long, c int)

func main {
    printf("sizeof One == %zu\n", sizeof One)
    printf("sizeof Two == %zu\n", sizeof Two)
}

Possible Output

sizeof One == 24
sizeof Two == 16
Clone this wiki locally