🧠 Class 19: Init Function

Video Topic: init() Function in Go


πŸ”€ Code Written in This Class

//example 1
package main

import "fmt"

func main() {
	fmt.Println("Hello Init Function!")
}

func init() {
	fmt.Println("I am the function that is executed first")
}
//example 2
package main

import "fmt"

var a = 10

func main() {
	fmt.Println(a)
}

func init() {
	fmt.Println(a)
	a = 20
}

πŸ” Key Concepts

  1. init() is a special Go function that runs before main(), automatically.

  2. You can have multiple init() functions across different files and packages. They all run in the order of:

    • Dependency packages first

    • File order (top to bottom) next

  3. You don't call init() manually. It runs automatically before the program starts.

🧠 CLI Memory & Execution Visualization (example 1)

Let’s visualize how Go handles init() under the hood:

// πŸ›  Compile Time: Go detects init()

Found init() in main package βœ…

----------- EXECUTION BEGINS -----------

🧠 Data Segment:
(none)

πŸ“š Stack:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 🧩 init()           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ–¨οΈ Output:
"I am the function that is executed first"

πŸ‘‹ init() returns

πŸ“š Stack:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 🧩 main()           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ–¨οΈ Output:
"Hello Init Function!"

βœ… Program ends gracefully

πŸ” CLI Visualization: Execution & Memory Layout (example 2)

=========== Program Compilation ===========
Found global variable: a = 10
Found init() βœ…
Found main() βœ…

=========== Execution Begins ==============

🧠 Data Segment (Globals):
a = 10 ← initialized before anything runs

πŸ“š Stack Frame:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  init()    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ” init() runs
β†’ Prints: 10
β†’ Updates a = 20

Stack after init():
(returns to runtime)

πŸ“š Stack Frame:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  main()    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ” main() runs
β†’ Prints: 20

=========== Execution Ends ================

πŸ“Œ Summary

    βœ… Global variable a is initialized before any function runs.

    βš™οΈ init() executes first:

        Reads a = 10

        Changes a = 20

    🧨 main() sees updated value: 20

This is a classic example of how init() can prepare or modify the runtime environment before the actual program logic in main() kicks in.

⚑ Quick Recap

  1. βœ… init() always runs before main() even if it’s written after main() in your code.

  2. ⛓️ You can use it to initialize configs, connections, default values, etc.

  3. πŸ’‘ A Go file can have at most one main(), but multiple init()s.

πŸ§ͺ "Init is like the secret backstage crew. You don’t see them during the show, but they’re the reason the lights come on."