🧭Struct
Also known as Structure (Programming), Record Type, Structs
A simple type that groups a few named values, like x and y of a point.
Think of it like
It is like a lunch box with set spots. One for the sandwich, one for the apple, one for the drink.
Example
A game keeps each bullet as a struct with x, y and speed. Thousands of them fit in memory and move fast.
Why it matters
Structs make small bits of data easy to pass around and name. In many languages they are also fast and light.
Definition
Example code
go
type Point struct {
X int
Y int
}
p := Point{X: 3, Y: 4}
fmt.Println(p.X)
// 3Common mistake
Key takeaways
- 1Named fields. Values grouped under one name.
- 2Mostly data. Few or no methods.
- 3Often copied. Passed as a copy in many languages.