Struct is a composite data type that groups named fields into a single value.

Structs are useful when several values belong together and should be treated as one thing. Unlike a tuple, a struct gives each field a name, which makes the data easier to read and less dependent on field order.

For example, in Golang:

package main
 
import "fmt"
 
type User struct {
	Name string
	Age  int
}
 
func main() {
	user := User{
		Name: "Ada",
		Age:  36,
	}
 
	fmt.Println(user.Name)
}

In this example, User is a struct type with two fields: Name and Age. The expression user.Name accesses the Name field on a specific User value.

Structs are useful for:

  • Modeling domain objects
  • Grouping related data
  • Passing several related values through a function as one value
  • Defining the shape of data used by APIs, files, or databases

Structs are usually concrete data types, not abstract data types. Their fields and representation are part of how the type is defined.

Languages without structs

Some languages do not have a direct struct keyword, but they still have common ways to model the same idea.

In Python, a small structured value is often represented with a class, a dataclass, a dictionary, or a named tuple.

from dataclasses import dataclass
 
@dataclass
class User:
    name: str
    age: int
 
user = User(name="Ada", age=36)
print(user.name)

In Javascript, a plain object is commonly used as a struct-like value.

const user = {
  name: "Ada",
  age: 36,
}
 
console.log(user.name)

In Java, classes and records are commonly used for struct-like data. A class can hold named fields and behavior, while a record is a compact way to define an immutable data carrier.

record User(String name, int age) {}
 
var user = new User("Ada", 36);
System.out.println(user.name());

Anki

id: struct-definition deck: Computer Science::Data Types tags: data-types struct

Q: What is a struct? A: A struct is a composite data type that groups named fields into a single value.

id: struct-vs-tuple deck: Computer Science::Data Types tags: data-types struct tuple

Q: How does a struct differ from a tuple? A: A struct gives each field a name, while a tuple usually relies on the position of each value.

id: go-struct-fields deck: Computer Science::Data Types tags: data-types struct golang

Q: In Go, what does type User struct { Name string; Age int } define? A: It defines a struct type named User with two named fields: Name and Age.

id: struct-concrete-type deck: Computer Science::Data Types tags: data-types struct abstract-data-type

Q: Is a struct usually an abstract data type? A: No. A struct is usually a concrete data type because its fields and representation are part of how the type is defined.

id: struct-equivalents deck: Computer Science::Data Types tags: data-types struct python javascript java

Q: What are common struct-like alternatives in Python, JavaScript, and Java? A: Python often uses classes, dataclasses, dictionaries, or named tuples; JavaScript often uses plain objects; Java often uses classes or records.