Functions
Basic Syntax:
func functionName(parameters) returnType {
// function body
}
func→ keyword to declare a functionfunctionName→ name of the functionparameters→ input values (optional)returnType→ type of value returned (optional)
Multiple Return Values
func divide(a, b int) (int, int) {
quotient := a / b
remainder := a % b
return quotient, remainder
}
func main() {
q, r := divide(10, 3)
fmt.Println("Quotient:", q, "Remainder:", r)
}
init Function
The init function is a special function that is automatically executed:
- Before the execution of the
mainfunction. - After all package-level variables are initialized.
- Without being explicitly called by the programmer.
Key Characteristics of init:
- No parameters and no return values
- The function signature is always:
func init() { ... }
- The function signature is always:
- Automatic execution
- It runs automatically, you cannot call it manually.
- Order of execution
- If multiple
initfunctions exist in the same package (even in different files), they run in the order the files are compiled. - If packages import other packages, the
initfunctions of imported packages run before the importing package’sinit.
- If multiple
- Multiple
initfunctions allowed- You can define multiple
initfunctions in the same package. They will execute one after another.
- You can define multiple
- Common use cases:
- Initializing global variables
- Setting up connections (databases, APIs, etc.)
- Registering plugins
- Preloading data before
mainruns
Basic usage of init
package main
import "fmt"
// Global variables
var a string = initMessage()
// Function called during variable initialization
func initMessage() string {
fmt.Println("Initializing global variable")
return "Hello from global variable"
}
// init function
func init() {
fmt.Println("Inside init function")
}
// main function
func main() {
fmt.Println("Inside main function")
fmt.Println("Value of a:", a)
}
Explanation:
- First, Go initializes package-level variables (
a).initMessage()runs → printsInitializing global variable.
- Then
init()function executes → printsInside init function. - Finally,
main()runs:- Prints
Inside main function - Prints
Value of a: Hello from global variable
- Prints
Output will be:
Initializing global variable
Inside init function
Inside main function
Value of a: Hello from global variable
Multiple init Functions
package main
import "fmt"
func init() {
fmt.Println("Init function 1")
}
func init() {
fmt.Println("Init function 2")
}
func main() {
fmt.Println("Main function")
}
Output:
Init function 1
Init function 2
Main function
All init functions run before main.
init Across Multiple Packages
Folder Structure:
project/
├── main.go
└── helper/
└── helper.go
helper/helper.go
package helper
import "fmt"
func init() {
fmt.Println("Init from helper package")
}
func Greet() {
fmt.Println("Hello from helper package")
}
main.go
package main
import (
"fmt"
"project/helper"
)
func init() {
fmt.Println("Init from main package")
}
func main() {
fmt.Println("Main function")
helper.Greet()
}
Output:
Init from helper package
Init from main package
Main function
Hello from helper package
The init function in the imported package (helper) runs before the one in the main package.
When to Use init?
Good use cases:
- Setup logging
- Initialize database connections
- Validate environment variables
- Preload configuration files
Avoid:
- Complex logic (hard to debug)
- Business logic (keep it in main)