Class 27: Structs & Memory Layout in Go 🧱

Welcome to Class 27! Today we're diving into structs, how to define and instantiate them, and how they interact with Go's memory model. Let's visualize everything from scratch like pros. πŸ§ πŸ’‘


✍️ The Code

package main

import "fmt"

type User struct {
	Name string
	Age  int
}

func (usr User) printDetails() {
	fmt.Println("Name:", usr.Name)
	fmt.Println("Age:", usr.Age)
}

func main() {
	user1 := User{
		Name: "Ruhin",
		Age:  21,
	}

	user2 := User{
		Name: "Mukim",
		Age:  15,
	}

	user1.printDetails()
	user2.printDetails()
}

🧠 Key Concepts

🧩 What is a Struct?

A struct is a user-defined type in Go used to group related data together. It’s like a custom container for fields.

type User struct {
	Name string
	Age  int
}

This defines a new type called User with fields Name and Age.


πŸ”¨ Creating Instances (Instantiation)

When we create an actual value using a struct type, that’s called instantiating.

user1 := User{
	Name: "Ruhin",
	Age:  21,
}

Here user1 is an instance of User. This allocates memory to hold Name and Age values.


🧠 Memory Layout (Visualization)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Code Segment          β”‚
β”‚-----------------------------β”‚
β”‚ main, printDetails,         β”‚
β”‚ type User struct {...}      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Data Segment          β”‚
β”‚-----------------------------β”‚
β”‚ -                           β”‚
β”‚ (Global vars if present)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Stack             β”‚
β”‚-----------------------------β”‚
β”‚ main() frame β†’              β”‚
β”‚   user1 β†’ Name: "Ruhin"     β”‚
β”‚           Age: 21           β”‚
β”‚   user2 β†’ Name: "Mukim"     β”‚
β”‚           Age: 15           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

⚠️ NOTE: If a struct is returned from a function or captured by a closure, it may escape to the heap instead of stack.


πŸ“‹ Example Use Case

type Book struct {
	Title  string
	Author string
	Pages  int
}

book1 := Book{
	Title: "1984",
	Author: "George Orwell",
	Pages: 328,
}

This lets us build real-world models with multiple fields.


🧹 Role of the Garbage Collector (GC)

  • If a struct instance escapes (used outside the function, stored long-term, etc.), Go stores it on the heap.
  • Go’s garbage collector then tracks and cleans it when it’s no longer in use.
  • This means you don’t have to manually free() anything β€” Go handles memory cleanup for heap objects.

πŸš€ TL;DR

  • type User struct {...} is metadata β†’ stored in the Code Segment.
  • user1 := User{...} is runtime data β†’ stored in Stack or Heap depending on usage.
  • Structs bundle fields into one logical unit βœ…
  • Memory layout varies depending on usage β†’ escape analysis decides πŸ“¦πŸ§³
  • GC only manages objects in the heap, not on the stack 🧹

Q: Is struct a datatype?

Ans: Yes, 100% β€” a struct in Go is a user-defined data type. Think of it like creating your own custom "blueprint" for a data object. πŸ’‘

Here's how it fits in:

-[] Go has primitive data types like int, string, bool, etc.

-[] You can then use struct to define a custom data type that groups multiple fields together.

For example:

type User struct {
	Name string
	Age  int
}

This User struct becomes its own data type, and now you can create instances of it just like you would for int or string:

var u User
u.Name = "Ruhin"
u.Age = 21

It’s like building your own Lego brick with a custom shape, and then making as many copies of that brick as you want. 🧱✨

You’re now struct-urally sound in Go! 😎 Next time you model data, flex your type muscles and track those memory segments like a boss.