Struct Fields

Accessing, annotating, and understanding the memory layout of fields in Go structs

A struct is a collection of named values, each of a specific type. Those values are its fields. Every struct you define, whether it models a person, a database row, or a configuration object, is only as useful as the fields it carries. This section moves beyond the basic declaration of structs and focuses on three practical aspects of fields: how you read and write them in code, how you annotate them with metadata for tools like encoding/json, and why their physical order in memory can affect the size of your data structures.

Field Access

How the dot operator reads and writes fields on values and pointers, how Go automatically dereferences pointers for you, and how the case of the first letter of a field name controls whether other packages can see it.

Struct Tags

How to attach metadata to fields with backtick-quoted tags, how encoding/json and encoding/xml read those tags, and how to read tags yourself with the reflect package for validation and other tooling.

Field Alignment and Padding

Why the compiler inserts invisible padding bytes between fields, how to measure a struct's real size and layout with the unsafe package, and how reordering fields from largest to smallest alignment shrinks the struct.

Field Access in Go Structs

Learn how to read and write individual struct fields in Go using the dot operator, the rules for exported and unexported fields.

Struct Tags

How Go struct tags work, why they exist, and how to use them with the standard library, reflection, and custom tooling.

Field Alignment and Padding

Understanding how Go arranges struct fields in memory, why padding bytes exist, and how to optimize field ordering for size and performance.