100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Structs in Go

A composite type that groups related fields together, forming the basis for custom data modeling in Go.

Data StructuresBeginner8 min readJul 8, 2026
Analogies

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

go
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

go
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

go
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

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#StructsInGo#Structs#Syntax#Explanation#Example#StudyNotes#SkillVeris