Initialization Order of Golang

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed. Init Each package can define one or more init function 2 (prototype is func init ()), the Init function is called when the package is first imported, the sequence of execution of multiple init functions within the same package is variable, and if the package imports another package, the Cascade call, All packages import is complete, and after all the Init functions are executed, the execution of main begins.  And for global variables, take a simple example to illustrate: //package P var gInt int ...  //Package a import "P" ...  //package B import "P" ...  //package main import ("a" "B") ... in package p, we define a global variable gint, and P is import by the package A, B, and then packagemain and then import a A, B, which is the import of a in front. A imports the P first, so at this point the Gint is initialized, which solves the problem of inconsistent initialization order of global variables in C + +.
[1] Golang Evaluation report: HTTP://WENKU.BAIDU.COM/VIEW/E14D70C50508763230121200 . html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.