In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "import import package and variable initialization method in Go", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "import import package and variable initialization method in Go" article.
Import import package search path
Import is used to import packages:
Import ("fmt", "net/http", "mypkg")
The compiler searches for the package according to the relative path specified above and then imports it, which starts under src under GOROOT or GOPATH (workspace).
If the installation directory of go is / usr/local/go, that is, GOROOT=/usr/local/go, and the GOPATH environment variable GOPATH=~/mycode:~/mylib, the search for the net/http package will be done in the following order:
/ usr/local/go/srcnet/http~/mycode/src/net/http~/mylib/src/net/http
The following is an error message when go install cannot find the mypkg package:
Can't load package: package mypkg: cannot find package "mypkg" in any of: / usr/lib/go-1.6/src/mypkg (from $GOROOT) / golang/src/mypkg (from $GOPATH)
In other words, go always searches first from GOROOT, then from the order of paths listed by GOPATH, and stops as soon as it finds the right package. When no package is found after the search, an error will be reported.
After the package is imported, you can use the properties in the package. Use the package name. Property is fine. For example, call the Println function fmt.Println in the fmt package.
The process of package import
Start with the main package. If there are import statements in the main package, these packages will be imported, and if the packages to be imported have packages to be imported, continue to import the dependent packages first. Duplicate packages are imported only once, just as many packages are imported into fmt packages, but it is imported only once.
After each imported package is imported, the exportable functions (beginning with uppercase letters), package variables, package constants, etc. of the package are declared and initialized, and then if the init () function is defined in the package, the init () function is automatically called. After the init () function call is complete, it returns to the package where the importer is located. Similarly, the importer's package has the same processing logic, declaring and initializing package variables, package constants, etc., then calling the init () function (if any), and so on, until returning to the main package, the main package will also initialize package constants, package variables, functions, and then call the init () function. After calling init (), call the main function, and then begin to enter the execution logic of the main program.
Alias import and special import methods
What happens when the package you want to import has the same name? For example, the network/convert package is used to transform the data read from the network, and the file/convert package is used to transform the data read from the file. If you want to import them at the same time, specify convert.FUNC () when referencing. Which package is this convert?
You can add a name attribute to the imported package and set an alias for the package. For example, in addition to importing the fmt package of the standard library, you also define a mypkg/fmt package, so you can import it as follows:
Package mainimport ("fmt" myfmt "mypkg/fmt") func main () {fmt.Println () myfmt.myfunc () / / access using aliases}
If you don't want to add the package name when accessing the package properties, you can set a special alias for it when import imports: dot (.).
Import (. "fmt") func main () {Println () / / access Println directly without package name
To access properties in fmt at this point, you must not use the package name fmt.
Go requires that packages imported by import must be used later, otherwise an error will be reported. If you want to avoid this error, underline the front of the package:
Import ("fmt" _ "net/http"mypkg")
This eliminates the need to use the net/http package in the current package. In fact, this is also a naming for the package, but named "_", and this symbol just means that the assignment result is discarded, making it an anonymous package.
The underscore (_) occurs very frequently in go. It is called blank identifier, and it can be used to discard values when assigning values, to preserve packets in import, and to discard the return value of functions. For details, please refer to the official manual: https://golang.org/doc/effective_go.html#blank
Importing without using may seem superfluous, but it is not. Because importing an anonymous package only means that the properties in it can no longer be accessed. However, when importing this anonymous package, some initialization operations (such as the init () function) are performed, and if this initialization operation affects the current package, then this anonymous import makes sense.
Remote packet
Now it is a major trend to share code through distributed version control systems. Go integrates the ability to obtain remote code from gti.
For example:
$go get github.com/golang/example
You can also use it in the import statement, first searching for a path from GOPATH, which is obviously a URL path, so call go get to fetch, and then import.
Import ("fmt"github.com/golang/example")
When you need to get the code from git, the go get tool will be called to automatically perform fetch, build, and install. If you already have this package in workspace, you will only proceed to the final install phase. If you do not have this package, it will be saved to the first path of GOPATH, and build, install.
Go get is recursive, so you can fetch the entire code tree directly.
Initialization of constants and variables
Constants in Go are created during compilation, even those local constants defined as functions. Constants can only be numeric, character (runes), string, or Boolean values.
Due to limitations during compilation, the expressions that define them must be constant expressions (constant expression) that can be evaluated by the compiler. For example, 1
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.