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.