Module lessons (1/5)
Define and use one struct
type Persona struct { Nome string; Eta int }, positional vs nominal initialization, struct anonymous.
What you'll learn in this lesson
- Define new compound types
type Name struct { ... }by grouping heterogeneous fields. - Initialize structs with named fields (avoiding fragile positional form), zero-value literals, and
&T{...}pointers. - Apply field visibility rules (uppercase/lowercase) and know when a struct is comparable to
==.
Overview
The struct is Go's fundamental block for aggregating related data in contiguous memory. Unlike classes in other languages, a struct contains only data: behaviors come via separately declared methods.
As with functions and variables, the first letter of each field governs its export to other packages. For disposable structures (e.g. configurations in table-driven tests) anonymous structs are used:
Sections covered in this lesson
- 1Secure declaration and initialization
- 2Visibility of fields and anonymous structs
- 3Comparability between structs with
== - 4Non-comparable structs and explicit comparison
- 5Guided practice
- 6Check for understanding
Unlock "Define and use one struct" and the full Course Go curriculum
`type Persona struct { Nome string; Eta int }`, positional vs nominal initialization, struct anonymous.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.