Introduction
A struct is a composite type that bundles zero or more named fields, each with its own type, into a single unit. Structs are the primary way Go models real-world entities and records, since the language has no classes. You define a struct type with the type keyword, create instances with struct literals, and access or modify fields with dot notation. Structs whose fields are all comparable can themselves be compared with ==, and struct embedding lets one struct promote another's fields and methods, though the full details of embedding are covered separately.
Cricket analogy: A struct is like a player's scorecard bundling named fields such as runs, balls faced, and strike rate into one record; you define the scorecard format once, fill in each player's literal, and compare two scorecards field by field with ==.
Syntax
type Point struct {
X, Y int
}
// Struct literal with field names
p1 := Point{X: 3, Y: 4}
// Positional literal (order must match field order)
p2 := Point{3, 4}
// Anonymous struct
config := struct {
Host string
Port int
}{Host: "localhost", Port: 8080}Explanation
type Point struct { X, Y int } declares a new named struct type with two int fields. Using field names in a literal, as in Point{X: 3, Y: 4}, is preferred because it is order-independent and remains correct if fields are reordered later; the positional form Point{3, 4} relies on field declaration order and breaks if that order changes. Anonymous structs, declared inline without a type name, are handy for short-lived, one-off groupings such as configuration passed to a single function. Two struct values of the same type are equal with == only if every corresponding field is equal, and only when all fields are themselves comparable.
Cricket analogy: Point{X: 3, Y: 4} naming fields is like filling a scorecard by labeled column, safe even if columns get reordered next season, while Point{3, 4} positional entry breaks if the column order changes; anonymous structs are like a quick handwritten note for a single over's stats, and two scorecards are equal with == only if every stat matches.
Example
package main
import "fmt"
type Point struct {
X, Y int
}
func main() {
a := Point{X: 1, Y: 2}
b := Point{X: 1, Y: 2}
c := Point{X: 5, Y: 5}
fmt.Println("a == b:", a == b)
fmt.Println("a == c:", a == c)
a.X = 10
fmt.Println("a after mutation:", a)
fmt.Println("b unaffected:", b)
}Output
a == b: true
a == c: false
a after mutation: {10 2}
b unaffected: {1 2}Key Takeaways
- A struct groups named fields of possibly different types into one type.
- Named-field literals are order-independent and safer than positional literals.
- Structs are value types: assignment and function calls copy all fields.
- Structs with only comparable fields can be compared directly with ==.
- Anonymous structs are useful for quick, one-off data groupings without a named type.
Practice what you learned
1. What is the safer way to initialize a struct literal and why?
2. Given two Point values with identical field values, what does a == b evaluate to?
3. What happens to variable b when a := Point{1,2}; b := a; a.X = 99 is executed?
4. When is an anonymous struct most appropriate?
5. Which struct fields prevent a struct type from being comparable with ==?
Was this page helpful?
You May Also Like
Arrays in Go
A fixed-size, ordered collection of elements of the same type whose length is part of its type.
Pointers in Go
A variable that holds the memory address of another value, enabling shared access and mutation without copying.
Struct and Interface Embedding in Go
Understand how Go uses struct and interface embedding to compose behavior instead of using inheritance.
Methods in Go
Learn how to attach methods to types in Go using value and pointer receivers.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics