In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to understand the underline of golang import". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the underline of golang import".
This operation is often an operator that is puzzling to many people. Take a look at the following import
Import ("database/sql" _ "github.com/ziutek/mymysql/godrv") / / _ operation actually introduces the package instead of directly using the functions in the package, but calls the init function in the package. / / to understand this problem, you need to see the following figure to understand how the packages are loaded sequentially:
Both initialization and execution of the program start with 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, then 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 are imported first, then the package-level constants and variables in those packages are initialized, then the init function (if any) is executed, and so on.
When all imported packages are loaded, the package-level constants and variables in the main package are initialized, then the init function (if any) in the main package is executed, and finally the main function is executed.
Through the above introduction, we understand that when import, it actually executes the init function in the package and initializes the variables inside. The _ operation only says that the package has been introduced. I only initialize the init function and some variables in the package, but often these init functions register the engine in the package, so that the outside can easily use it. Many of these init functions are caused by database/sql. In the init function, you call sql.Register (name string, driver driver.Driver) to register yourself, and then you can use it externally.
Thank you for your reading, the above is the content of "how to understand the underline of golang import". After the study of this article, I believe you have a deeper understanding of how to understand the underline of golang import, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.