While learning Go, I came across a few functions that behave differently from the functions we've been writing so far.
We already talked about main() in Part 1, but there are two other things in my notes that I wanted to understand better: init() and defer.
They all have something to do with when code runs, but each one has a different role.
main()
Let's start with the one we already know.
Every executable Go program has a main package, and execution eventually starts by calling the main() function.
package main import "fmt" func main() { fmt.Println("Hello World") } The main() function:
- Takes no arguments.
- Returns no values.
- Acts as the entry point of the executable program.
When main() returns, the program exits.
Coming from Ruby, this is different from how I normally write scripts:
puts "Hello World" Ruby doesn't require me to define a main function. I can simply write code in the file, and it executes when I run it.
Go makes the entry point explicit.
init()
Go has a special function called init() that runs automatically before main().
It's used for initialization work that needs to happen before the program starts executing main.
package main import "fmt" func init() { fmt.Println("Initializing...") } func main() { fmt.Println("Main function") } Output:
Initializing... Main function We don't call init() ourselves.
Go runs it automatically as part of package initialization, before main().
Can We Have More Than One init()?
Yes!
A package can have multiple init() functions, even within the same file.
package main import "fmt" func init() { fmt.Println("First init") } func init() { fmt.Println("Second init") } func main() { fmt.Println("Main") } They run during initialization before main().
One thing to remember is that init() isn't something we call whenever we need it. It's a special initialization function, and Go invokes it automatically.
defer
This one I found most interesting.
The defer keyword delays the execution of a function until the surrounding function returns.
For example:
package main import "fmt" func main() { defer fmt.Println("World") fmt.Println("Hello") } Output:
Hello World Although we wrote the defer statement before Hello, the deferred call executes when main() returns.
So instead of running immediately, Go postpones it.
Why Would We Use defer?
A common use case is cleanup.
For example, when working with files:
file, err := os.Open("example.txt") if err != nil { return } defer file.Close() Once we successfully open the file, we defer closing it.
That means when the surrounding function returns, file.Close() will be executed.
This is useful because we don't have to remember to close the file at every possible exit point in the function.
We can register the cleanup immediately after acquiring the resource.
Deferred Calls Run in LIFO Order
deferred calls are executed in reverse order.
LIFO means Last In, First Out.
func main() { defer fmt.Println("First") defer fmt.Println("Second") defer fmt.Println("Third") fmt.Println("Hello") } Output:
Hello Third Second First The last deferred function we registered runs first.
This is useful when we have multiple cleanup operations that need to happen in a specific order.
What About Ruby?
Ruby doesn't have a direct equivalent of Go's defer keyword, but we have similar patterns depending on what we're trying to achieve.
For example, when working with resources, Ruby uses ensure:
file = File.open("example.txt") begin puts file.read ensure file.close end The ensure block runs whether the code succeeds or raises an exception.
This registers code to run when the Ruby program exits.
That feels closer to deferring work until program termination, but Go's defer is tied to the return of the surrounding function, not necessarily the entire program.
That's an important distinction.
Go vs Ruby
| Concept | Go | Ruby |
|---|---|---|
| Entry point | main() in package main | File execution begins directly |
| Initialization | init() | Initialization code / class or module setup |
| Automatic initialization | init() runs automatically | No direct equivalent |
| Delayed execution | defer | ensure, at_exit, blocks depending on use |
| Cleanup | defer file.Close() | ensure / resource management patterns |
| Multiple deferred calls | LIFO order | Depends on the construct |
The Main Idea
These three functions and keywords serve different purposes:
-
main()is where the executable program starts. -
init()runs automatically beforemain()to initialize things. -
deferpostpones a function call until the surrounding function returns.
Coming from Ruby, I like that defer makes cleanup code easy to keep close to where we acquire the resource.
Instead of remembering to close a file at the end of the function, I can simply defer it immediately after opening it.
And that's another Go concept that initially felt unfamiliar but makes a lot of sense once you understand when it runs.

