Skip to content
BENIBIX Learn

Struct

Development🧭 Programming ParadigmsObject-Oriented Programming

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

A struct groups a few named values into one new type. A Point struct might hold x and y. It is like a simple class, often with data and few or no methods. C, C#, Go, Rust and Swift all have structs. In C#, Swift and Go, a struct is usually copied when you pass it, while a class object is shared.

Example code

go
type Point struct {
    X int
    Y int
}

p := Point{X: 3, Y: 4}
fmt.Println(p.X)
// 3
A Point struct holds two named numbers, read with a dot.
A struct holds x and y together, as one named box.

Common mistake

Assuming a struct acts like a class object. In C# and Swift, a changed copy does not change the first one.

Key takeaways

  1. 1
    Named fields. Values grouped under one name.
  2. 2
    Mostly data. Few or no methods.
  3. 3
    Often copied. Passed as a copy in many languages.

Compare