Skip to main content

Functions

Basic Syntax:

func functionName(parameters) returnType {
// function body
}
  • func → keyword to declare a function
  • functionName → name of the function
  • parameters → 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 main function.
  • After all package-level variables are initialized.
  • Without being explicitly called by the programmer.

Key Characteristics of init:

  1. No parameters and no return values
    • The function signature is always:
      func init() { ... }
  2. Automatic execution
    • It runs automatically, you cannot call it manually.
  3. Order of execution
    • If multiple init functions exist in the same package (even in different files), they run in the order the files are compiled.
    • If packages import other packages, the init functions of imported packages run before the importing package’s init.
  4. Multiple init functions allowed
    • You can define multiple init functions in the same package. They will execute one after another.
  5. Common use cases:
    • Initializing global variables
    • Setting up connections (databases, APIs, etc.)
    • Registering plugins
    • Preloading data before main runs

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:

  1. First, Go initializes package-level variables (a).
    • initMessage() runs → prints Initializing global variable.
  2. Then init() function executes → prints Inside init function.
  3. Finally, main() runs:
    • Prints Inside main function
    • Prints Value of a: Hello from global variable

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)