In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
We often use the import command to import package files when writing Go code, and the ways we often see are as follows:
Import ("fmt")
Then we can call the code in the following way
Fmt.Println ("hello world")
The above fmt is the standard library of the Go language. He actually goes to goroot to load the module. Of course, Go's import also supports the following two ways to load the module written by him:
Relative path
Import ". / model" / / the model directory where the current files are in the same directory, but it is not recommended to import in this way
Absolute path
Import "shorturl/model" / / load gopath/src/shorturl/model module
The above shows some common ways of import, but there are also some special import, which is very difficult for many beginners to understand. Let's explain what's going on one by one.
Point operation
We sometimes see the following ways to import packages
Import (. The meaning of this dot operation is that after the package is imported, when you call the package's function, you can omit the package name of the prefix, that is, the fmt.Println ("hello world") you called earlier can be omitted as Println ("hello world").
Alias operation
Alias operation as the name implies, we can name the package another name that we can easily remember.
If import (f "fmt") alias operation, the prefix becomes our prefix when calling the package function, that is, f.Println ("hello world")
_ operation
This operation is often an operator that is puzzling to many people. Take a look at the following import
The 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 look at the following figure to understand how the packages are loaded in order:
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. The following figure explains the entire execution process in detail:
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.
In this way, we have introduced all the import. I hope it will be helpful for you to understand the import of Go.
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
The difference between hard link and soft link file
© 2024 shulou.com SLNews company. All rights reserved.