Struct Basics in Go
Learn what structs are in Go, how to declare struct types, create struct literals with different initialization styles.
A struct is Go's way of creating your own data types by bundling together named values that belong together. If you have ever filled out a form with fields for a name, an address, and a phone number, you already have the right mental model: a struct is that form, and each field holds one piece of the larger whole. Rather than juggling separate variables that are only meaningful as a group, you define a struct that carries them all as a single value you can pass around, store, and reason about as one thing.
Go's structs are deliberately simple. They do not support inheritance, and they do not automatically generate methods the way classes do in other languages. What they give you is a clean, explicit container for related data with predictable behavior, nothing hidden, nothing magical.
Struct Type Declarations
How to declare a named struct type with the type keyword, what each field's name and type mean, how exported and unexported fields differ, and when an anonymous struct type is the right choice.
Struct Literals
How to create struct values with positional and keyed literals, when each style fits, how nested literals work, and why keyed literals survive changes to the struct definition that break positional ones.
Zero Value of Structs
What a struct looks like before you fill it in, why every field starts at a predictable zero value, and which zero-value fields (like nil maps) still need attention before you use them.
Struct Type Declarations
Learn how to declare struct types in Go, define fields with names and types, and use anonymous structs for one-off data grouping.
Struct Literals
Create and initialize struct values in Go using positional and keyed struct literals, including nested literals and field type elision.
Zero Value of Structs
Understanding how Go initializes struct fields to their zero values, why it matters, and how to use it safely.