Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Package Initialization

Tags:

Situation:

A Go package A is composed of 3 .go files, and I use functions from another package B in each of these files. I have to import package B at the beginning of each file.

Question:

Is package B actually initialized 3 times or only 1 time?

like image 559
Seth Archer Brown Avatar asked Jul 18 '13 20:07

Seth Archer Brown


People also ask

What is package initialization?

Package initialization section as the name suggest is executed when package is initialized. This happens when first procedure/function from the package is executed after session is established or after package is (re)compiled.

How do you initialize Go?

Open a command prompt and cd to your home directory. Create a greetings directory for your Go module source code. Start your module using the go mod init command. Run the go mod init command, giving it your module path -- here, use example.com/greetings .

Should we use init in Golang?

Golang init() In Go, init() is a very special function meant to execute prior to the main() function. Unlike main(), there can be more than one init() function in a single or multiple files.

How do I create a Go package?

Check your GOPATH in environment variables and set it to the directory which contains all Go files. Create a new folder with the name of the package you wish to create. In the folder created in step 2, create your go file that holds the Go package code you wish to create.


1 Answers

Short answer: Initialization will be performed only once.

Long answer: Quoting the relevant specification section - Program execution:

A package with no imports is initialized by assigning initial values to all its package-level variables and then calling any package-level function with the name and signature of

func init() 

defined in its source. A package-scope or file-scope identifier with name init may only be declared to be a function with this signature. Multiple such functions may be defined, even within a single source file; they execute in unspecified order.

Within a package, package-level variables are initialized, and constant values are determined, according to order of reference: if the initializer of A depends on B, A will be set after B. Dependency analysis does not depend on the actual values of the items being initialized, only on their appearance in the source. A depends on B if the value of A contains a mention of B, contains a value whose initializer mentions B, or mentions a function that mentions B, recursively. It is an error if such dependencies form a cycle. If two items are not interdependent, they will be initialized in the order they appear in the source, possibly in multiple files, as presented to the compiler. Since the dependency analysis is done per package, it can produce unspecified results if A's initializer calls a function defined in another package that refers to B.

An init function cannot be referred to from anywhere in a program. In particular, init cannot be called explicitly, nor can a pointer to init be assigned to a function variable.

If a package has imports, the imported packages are initialized before initializing the package itself. If multiple packages import a package P, P will be initialized only once.

The importing of packages, by construction, guarantees that there can be no cyclic dependencies in initialization.

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

func main() { … } 

Program execution begins by initializing the main package and then invoking the function main. When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.

Package initialization—variable initialization and the invocation of init functions—happens in a single goroutine, sequentially, one package at a time. An init function may launch other goroutines, which can run concurrently with the initialization code. However, initialization always sequences the init functions: it will not start the next init until the previous one has returned.

like image 81
zzzz Avatar answered Oct 05 '22 22:10

zzzz