GoLang Series
Hello GoLang: https://blog.yexca.net/en/archives/154
GoLang (var and const) Variables and Constants: https://blog.yexca.net/en/archives/155
GoLang (func) Functions: This article
GoLang (slice and map) Slices: https://blog.yexca.net/en/archives/160
GoLang (OOP) Object-Oriented: https://blog.yexca.net/en/archives/162
GoLang (reflect) Reflection: https://blog.yexca.net/en/archives/204
GoLang (struct tag) Struct Tags: https://blog.yexca.net/en/archives/205
GoLang (goroutine) Goroutines: https://blog.yexca.net/en/archives/206
GoLang (channel) Channels: https://blog.yexca.net/en/archives/207
Multiple Return Values
Go functions can return multiple values.
Anonymous Return
| |
Named Parameter Return
| |
If the return types are the same, they can be merged:
| |
init and main
The init function can exist in any package and appear multiple times in the same package, though one is recommended.
The main function must be in package main, and that package must contain the function.
Both are reserved functions; they take no arguments and return no values.
Go automatically calls init() and main().
Program Execution
Program initialization and execution start from the main package. Even if a package is imported by multiple packages, it is only imported once. Here is the execution order:

Example:
Assume the following structure:
| |
Contents:
lib1.go
| |
lib2.go
| |
main.go
| |
Execution result:
| |
Now, import Lib2 into Lib1, keeping the main code the same:
lib1.go
| |
Running main results in:
| |
Notice lib2 only initializes once.
Calling Functions from Other Packages
In the previous example, _ was used as an anonymous alias, meaning you couldn’t call methods from that package.
Add a function to lib1:
lib1.go
| |
main.go
| |
Alternatively, you can use ., in main.go:
| |
Not recommended; it creates ambiguity if two packages have functions with the same name.
Pointers
Similar to C pointers.
Parameters can be passed by value or by pointer (reference). By default, Go uses pass-by-value (as seen in the first section of this post).
Use & to get a variable’s memory address.
| |
Passing by reference sends the memory address to the function; modifications affect the original variable. Here’s the swap function again, using pointers:
| |
defer
The defer statement schedules a function call to run just before the surrounding function returns. It is often used for:
- Releasing resources
- Catching and handling panics
- Logging
It’s similar to finally in a try...catch...finally block.
Commonly used for paired operations like opening/closing files, acquiring/releasing locks, or connecting/disconnecting. It ensures resources are cleaned up even if an error occurs or the function returns early.
If a function has multiple defer statements, they are executed in LIFO (Last-In-First-Out) order, like a stack.
| |
recover
A runtime panic will crash the program. recover is a built-in function used to “intercept” a panic, similar to a catch block in Java.
recoveris only effective when called inside adeferredfunction.
| |