๐๏ธ Building with Blueprints: Go Structs Basics
Imagine youโre building with LEGOยฎ. Before you snap pieces together, you need a planโa blueprint that says โthis goes here, that goes there.โ In Go, that blueprint is called a Struct.
๐ฏ The Big Picture
A struct (short for โstructureโ) is like a recipe card or a form you fill out. It groups related information together under one name.
Real-life example:
- Your ID card has: Name, Photo, Birthday, Address
- A struct does the same thing for your code!
graph TD A["๐ชช ID Card"] --> B["Name: Alex"] A --> C["Age: 10"] A --> D["School: Maple Elementary"]
๐ฆ 1. Struct Declaration โ Creating Your Blueprint
What is it?
A struct declaration is like drawing up a plan. Youโre telling Go: โI want to group these pieces of information together.โ
The Recipe
type Person struct {
Name string
Age int
}
Breaking it down:
typeโ โIโm creating a new typeโPersonโ The name of your blueprintstructโ โItโs a structureโ{ }โ Everything inside is part of the plan
๐ง Think of it like this:
Youโre creating a form template. The form has blank spaces for โNameโ and โAgeโ. You havenโt filled it in yetโyou just designed the form!
๐ท๏ธ 2. Struct Fields โ The Blank Spaces
What are fields?
Fields are the individual pieces of information inside your struct. Theyโre like the blank lines on a form.
type Dog struct {
Name string // Field 1
Breed string // Field 2
Age int // Field 3
Fluffy bool // Field 4
}
Each field has:
- A name โ What to call it (
Name,Breed) - A type โ What kind of info it holds (
string,int,bool)
๐จ Visual:
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DOG FORM โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Name: ____________ โ
โ Breed: ____________ โ
โ Age: ____________ โ
โ Fluffy: โก Yes โก No โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐๏ธ 3. Struct Initialization โ Filling Out the Form
Now the fun part! You have your blueprintโletโs fill it in!
Method 1: Named Fields (Clear & Safe) โ
myDog := Dog{
Name: "Buddy",
Breed: "Golden",
Age: 3,
Fluffy: true,
}
Method 2: Ordered Values (Quick but risky) โ ๏ธ
myDog := Dog{"Buddy", "Golden", 3, true}
Order must match exactly!
Method 3: Start Empty, Fill Later
var myDog Dog
myDog.Name = "Buddy"
myDog.Age = 3
Method 4: Zero Values (Empty Form)
var emptyDog Dog
// Name = "" (empty string)
// Age = 0
// Fluffy = false
๐ฐ Like ordering a cake:
- Named fields: โI want chocolate flavor, vanilla frosting, 2 layersโ
- Ordered values: โChocolate, vanilla, 2โ (hope you remember the order!)
๐ 4. Struct Copy Behavior โ The Photocopier Rule
โก The Surprise:
When you copy a struct, Go makes a complete photocopy. Changing the copy does NOT change the original!
original := Dog{Name: "Max", Age: 5}
copy := original // Full photocopy!
copy.Name = "Rocky" // Only copy changes
// original.Name is still "Max"!
// copy.Name is "Rocky"
graph LR A["๐ Original<br/>Name: Max"] -->|Copy| B["๐ Copy<br/>Name: Max"] B -->|Change| C["๐ Copy<br/>Name: Rocky"] A -.->|Unchanged!| A
๐จ๏ธ Think of it like:
You photocopy a homework sheet. Your friend writes on their copy. Your original is still clean!
Why does this matter?
func birthday(d Dog) {
d.Age = d.Age + 1 // This changes
} // the COPY only!
myDog := Dog{Name: "Luna", Age: 2}
birthday(myDog)
// myDog.Age is still 2! ๐ฑ
The function got a photocopy, not the real thing!
๐ญ 5. Anonymous Structs โ Quick & Disposable
What are they?
Sometimes you need a quick struct for ONE thing. No need to name itโjust use it!
Example 1: One-time data
point := struct {
X int
Y int
}{10, 20}
Example 2: Quick configuration
config := struct {
Debug bool
Version string
}{
Debug: true,
Version: "1.0",
}
๐ฅก Think of it like:
- Named struct: A reusable plastic container with a label
- Anonymous struct: A paper plateโuse once and done!
When to use:
โ Quick tests โ One-time data grouping โ Simple configurations โ Donโt use if you need it more than once!
๐ Quick Recap
| Concept | What it does | Everyday example |
|---|---|---|
| Declaration | Creates the blueprint | Drawing a form template |
| Fields | The blank spaces | Lines to fill in |
| Initialization | Fills in the form | Writing your info |
| Copy Behavior | Makes photocopies | Copying homework |
| Anonymous | Quick throwaway struct | Paper plate |
๐ Your First Complete Example
package main
import "fmt"
// 1. Declaration
type Student struct {
Name string
Grade int
Pass bool
}
func main() {
// 3. Initialization
alex := Student{
Name: "Alex",
Grade: 95,
Pass: true,
}
// 4. Copy behavior
backup := alex
backup.Grade = 100
fmt.Println(alex.Grade) // 95
fmt.Println(backup.Grade) // 100
// 5. Anonymous struct
temp := struct {
X, Y int
}{5, 10}
fmt.Println(temp.X) // 5
}
๐ก Remember This!
Structs are like ID cards for your data. They keep related information together, neat and organized. And when you copy them, you get a whole new cardโnot just a reference to the same one!
Youโve got this! ๐ฏ
