Skip to main content

Command Palette

Search for a command to run...

Init Function

Updated
2 min readView as Markdown
Init Function
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

init Function

Identifier main is ubiquitous. Every Go program starts in a package main by calling identically named function. When this function returns the program end its execution. Functions init also play special role which are defined in package block and are usually used for:

  • variables initialization if cannot be done in initialization expression.
  • registering.
  • running one-time computations.

init() function will be executed first before main function

package main

import "fmt"

func main() {
    fmt.Println("main() will run second")
}

func init() {
    fmt.Println("init() will run first")
}

playground

If we run the code it will give us result

Output
init() will run first
main() will run second

init() function to use for variables initialization.

given two examples, the first example is code without init() function.


package main

import "fmt"

var weekday string

func main() {
    fmt.Printf("Today is %s", weekday)
}

playground

In this example, we declared a global variable called weekday. By default, the value of weekday is an empty string.

Because the value of weekday is blank, when we run the program, we will get the following output:

Output
Today is

We can fill in the blank variable by introducing an init() function that initializes the value of weekday to the current day.

the second example is code with init() function.

package main

import (
    "fmt"
    "time"
)

var weekday string

func init() {
    weekday = time.Now().Weekday().String()
}

func main() {
    fmt.Printf("Today is %s", weekday)
}

playground

Now when we run the program, it will print out the current weekday:

Output
Today is Tuesday

init() as properties

init function doesn't take arguments neither returns any value. In contrast to main, identifier init is not declared so cannot be referenced.

package main

import (
    "fmt"
)

var weekday string

func init() {
    fmt.Println("hello init")
}

func main() {
    init()
}

playground

When we run it will give the result of error

/prog.go:14:5: undefined: init

Many init function can be defined in the same package or file

package main

import "fmt"

func init() {
    fmt.Println("init in 1")
}


func init() {
    fmt.Println("init in 2")
}

func main() {
    fmt.Println("main")
}

playground

When we run the code it will give the result

Output
init in 1
init in 2
main
51 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !