Init Function

Init Function

In this tutorial, we are going to discuss the init function on the Go language. init() function is a special function that is used to initialize global variables of a package. These functions are executed when the package is initialized.

init function

Each of the GO source files in a package can have it’s own init() function. Whenever you import any package in the program, then on the execution of that program, init functions(if present) in the GO source files belonging to that imported package are called first.

Some points to note about the init function

  • Init function is optional
  • Init function does not take any argument
  • Init function does not have any return value.
  • Init function is called implicitly. Since it is called implicitly, init function cannot reference it from anywhere.
  • There can be multiple init() functions within the same source file.

In general Applications have initialization requirements. And init() function is Golang’s solution to the initialization problem. This function is executed before main().

package db

import (
       "gopkg.in/mgo.v2"
       "gopkg.in/mgo.v2/bson"
)

func init {
   // initialization code here    
}

init function is majorly used to initialize global variables that cannot be initialized using an initialization expression.

For example, it requires a network call to initialize any DB client. Another example could be fetching secret keys on startup. Init function is also used for running anything that only needs to be executed once. Let’s see a simple use case of using an init function.

In some contexts, we may need to import a package only for invoking it’s init method, where we don’t need to call forth other methods of the package.

If we imported a package and are not using the package identifier in the program, Go compiler will show an error. In such a situation, we can use a blank identifier ( _ ) as the package alias name.

So the compiler ignores the error of not using the package identifier, but will still invoke the init function.

package main

import (
    _ "mywebapp/libs/mongodb/db"
      "fmt"
      "log"
)

func main() {
  //implementation here
}

In the above sample program, we imported a package named db. Let’s say we want to use this package only for calling the init method.

The blank identifier will enable us to avoid the error from Go compiler, and we will be able to invoke the init method defined in the package.

We can use alias names for packages to avoid package name ambiguity.

package main

import (
        mongo "mywebapp/libs/mongodb/db"
        mysql "mywebapp/libs/mysql/db"  	
)

func main() {
    mongodata :=mongo.Get() //calling method of package  "mywebapp/libs/mongodb/db"
    sqldata:= mysql.Get() //calling method of package "mywebapp/libs/mysql/db"  
    fmt.Println(mongodata )
    fmt.Println(sqldata )
}

We are importing two different packages from two different locations, but the name of both packages are the same.

So we can use an alias name for one package, and can use the alias name whenever we need to call a method of that package.

Features of init() function

1. There can be multiple init() functions in a package or even in a single file

2. The order of execution of multiple init()’s is decided by the order in which the go files are passed to go build command — It is recommended that the go files are passed to go build command in lexical order.

3. Within a single file, if multiple init() functions are defined, they will be invoked in the order they are defined.

4. If test file has an init() function, then that init() will be executed before the test is executed. In other words, first, all not-test files init() functions are executed, then all test files init() functions are executed.

5. Application programmer cannot invoke init() functions. That means they cannot test the function either.

6. Even if you initialize the package multiple times, init() function will be invoked only once.

That’s all about the Init Function in Go language. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Go language.!!

Init Function
Scroll to top