"Go Language Lesson 1" Course Study Notes (5)

Entry function and package initialization: figure out the execution order of Go programs

main.main function: the entry function of the Go application

  • There is a special function in the Go language: the main function in the main package, that is, main.main, which is the entry function of the user-level execution logic of all Go executable programs.
    • The execution logic of the Go program at the user level will be expanded in this function according to the order in which it is called.
    • The function prototype of the main function is very simple, with no parameters and no return value.
    • Go language requirement: the main package of the executable program must define the main function, otherwise the Go compiler will report an error. In a Go application that starts multiple Goroutines, the main.main function will be executed in the Go application's main Goroutine.
    • However, for the main function of the main package, one more thing needs to be clarified, that is, although it is the entry function of the user-level logic, it is not necessarily the first function executed by the user-level.

init function: the initialization function of the Go package

  • Go language also has a special function, which is the init function for package initialization. Like the main.main function, the init function is also a function with no parameters and no return value.
  • If the init function is defined in the package that the main package depends on, or if the main package itself defines the init function, then the Go program will automatically call its init function when the package is initialized, so the execution of these init functions will happen before the main function.
    • When initializing a Go package, Go will call the init function of this package one by one and sequentially according to a certain order.
    • Generally speaking, the init function in the source file passed to the Go compiler first will be executed first; while multiple init functions in the same source file will be executed in order of declaration.
    • When we want to execute some functions or statements before the main.main function is executed, we only need to put it into the init function.

Initialization order for Go packages

  • Go package is the basic unit of program logic encapsulation, and each package can be understood as an "autonomous", well-encapsulated basic unit that exposes limited interfaces to the outside.
    • A Go program is made up of a set of packages, and the initialization of the program is the initialization of these packages.
    • Each Go package will also have its own dependent packages, constants, variables, init functions (the main package has a main function), etc.
    • When we read and understand the code, we need to know the initialization order of these elements in the program initialization process, so that we can determine the current state of these elements at a certain line of code.
  • In the process of package initialization, Go will adopt the principle of "depth first" to recursively initialize the dependent packages of each package.
  • Go will be initialized in the order of "constant -> variable -> init function", and the function of the program will not be officially entered until the initialization work is completed. Multiple init functions within a package are automatically called in the order of appearance.

Purpose of the init function

  • The first common use of init functions: resetting package-level variable values. The init function is like the only "quality inspector" before the Go package is actually put into use. It is responsible for checking the initial state of the package-level data (mainly package-level variables) inside the package and exposed to the outside.
  • The second common use of the init function is to implement complex initialization of package-level variables.
  • The third common use of the init function: to implement the "registration mode" in the init function. By registering its own implementation mode in the init function, the direct exposure of the Go package to the outside world is effectively reduced.

Guess you like

Origin blog.csdn.net/fangzhan666/article/details/132336583