SoFunction
Updated on 2025-03-02

The specific use of Go language init function

init function

C language does not have an init function. C language generally needs to write init by itself and then call it in the constructor.

Go language comes with init functions, each package can contain one or more init functions

This init will be automatically called when the package is referenced (import).

Init function demonstration

The init function has no parameters and no return value. The prototype is fixed as follows

When a package contains multiple inits, the order of calls is uncertain.

Multiple files in the same package can have init functions.

Create subtraction function package

Create a test folder.

Create a subtraction file

Create a sub folder in the test folder, create it in it.

Examples are as follows:

package sub

import "fmt"

//The init function has no parameters and no return value. The prototype is fixed as follows// When a package contains multiple inits, the order of call is uncertain
func init()  {
   ("This is package sub The first one init()")
}

func init()  {
   ("This is package sub The second one init()")
}

func Sub(a, b int) int {
   return a - b
}

Create an entry file call

Create an import and call the subtraction function in the test folder.

Examples are as follows:

package main

import (
   "fmt"
   "test/sub"
)

func main() {
   res2 := (10, 6)
   ("10-6=", res2)
}

Running results:

This is the first init() in package sub
This is the second init() in package sub
10-6= 4

Create subtraction function

Create the same namespace function file sub2 in the subtraction function file and call it in the subtraction function.

In the go language, multiple package names are not allowed to appear at the same level of directories.

Since Sub2 test2 is under the same package, it can be used and does not require the sub. form.

Examples are as follows:

package sub

import "fmt"

func Sub2()  {
   ("Sub2 func")
}

func test2()  {
   ("test func")
}

Running results:

This is the first init() in package sub
This is the second init() in package sub
Sub2 func
test func
10-6= 4

_form

Init function is not allowed to be called explicitly by the user.

Sometimes when referring to a package, you may just want to use the init function in this package (mysq1iinit initializes the driver)

However, you do not want to refer to other functions in the package. In order to prevent compiler errors, you can use the _ form to handle it.

Examples are as follows:

package main

import (
   //"fmt"
   _ "test/sub" // Only the init function in sub will be called at this time, and there will be no errors in the compilation.)

func main() {
   //res2 := (10, 6)
   //("10-6=", res2)
}

Running results:

This is the first init() in package sub
This is the second init() in package sub

Summarize

This is the end of this article about the specific use of Go language init functions. For more relevant Go language init functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!