call of go/golang init() method

 

 

go/golang main() init() method call

u011156212 ·  2015-10-20 13:00:05 · 9965 hits ·  Estimated reading time 1 minute ·  27 minutes ago Start browsing    
This is an  article created on 2015-10-20 13:00:05, the information in it may have evolved or changed.

The main() and init() methods are the default two methods in go, two reserved keywords.

The init() method can appear in any package, but it is recommended that each package contain only one init() function, which is easy to understand.
But the main() method can only be used in package main.
Go programs automatically call init() and main(), so you don't need to call these two functions anywhere. The init function in each
package is optional, but the package main must contain a main function.
The initialization and execution of the program starts from the main package. If the main package also imports other packages, they will be
imported in turn at compile time. Sometimes a package will be imported by multiple packages at the same time, so it will only be imported once (for example, many packages
may use the fmt package, but it will only be imported once, because there is no need to import multiple times). When a package is imported,
if the package also imports other packages, the other packages will be imported first, then the package-level constants
and variables in these packages will be initialized, and then the init function (if any) will be executed. ),And so on. When all imported packages are
loaded , the package-level constants and variables in the main package will be initialized, then the
init function in the main package (if it exists) will be executed, and finally the main function will be executed.

 

 
 
 
 

The init function in the go language is used for the initialization of the package. This function is an important feature of the go language.

Has the following characteristics:

1 The init function is a function used to initialize the package before the program is executed, such as initializing the variables in the package, etc.

2 Each package can have multiple init functions

3 Each source file of a package can also have multiple init functions

4 The execution order of multiple init functions in the same package is not clearly defined in the go language (description)

5 The init functions of different packages determine the execution order of the initialization functions according to the dependencies of package imports

6 The init function cannot be called by other functions, but is automatically called before the main function is executed

The following example is taken from "the way to go", the os differences are hidden when the application is initialized,

var prompt = "Enter a digit, e.g. 3 " + "or %s to quit."

func init() {
    if runtime.GOOS == "windows" {
        prompt = fmt.Sprintf(prompt, "Ctrl+Z, Enter")
    } else { // Unix-like
        prompt = fmt.Sprintf(prompt, "Ctrl+D")
    }
}

The following two go files demonstrate:

 1 A package or go file can contain multiple init functions,

 2 The init function is executed before the main function,

 3 The init function is automatically called and cannot be called in other functions. Explicit calls will report that the function is undefined 

gprog.go code

package main

import (
    "fmt"
)

// the other init function in this go source file
func init() {
    fmt.Println("do in init")
}

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

func testf () {
    fmt.Println("do in testf")
    //if uncomment the next statment, then go build give error message : .\gprog.go:19: undefined: init
    //init()
}

ginit1.go code, note that there are two init functions in this source file

package main

import (
    "fmt"
)

// the first init function in this go source file
func init() {
    fmt.Println("do in init1")
}

// the second init function in this go source file
func init() {
    fmt.Println("do in init2")
}

Compile the above two files: go build gprog.go ginit1.go

The result of executing gprog.exe after compilation shows that the init function in gprog.go is executed first, then the two init functions in ginit1.go are executed, and then the main function is executed.

E:\opensource\go\prj\hellogo>gprog.exe
do in init
do in init1
do in init2
do in main

Note: "the way to go" (P70) has the following description in red, which means that a go source file can only have one init function,

      But the two init functions in the above ginit1.go are executed normally after compiling and running,

      So this sentence should be a typo.

4.4.5 Init-functions
Apart from global declaration with initialization, variables can also be initialized in an init()-function.
This is a special function with the name init() which cannot be called, but is executed automatically
before the main() function in package main or at the start of the import of the package that
contains it.
Every source file can contain only 1 init()-function. Initialization is always single-threaded and
package dependency guarantees correct execution order.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708695&siteId=291194637