In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What is a package in Go language
We are using other languages, such as Java, which have the concept of packages. It is a concept in the Java language to organize our Java files, such as the java.lang package, which contains many of our commonly used classes, such as String. Packages are a similar concept in the Go language. It organizes our Go files so that they can be easily classified, reused, etc., such as Go's built-in net package.
Net ├── http ├── internal ├── mail ├── rpc ├── smtp ├── testdata ├── textproto └── url
The above is a directory structure of the net package, net itself is a package, and http under the net directory is a package. As you can see from this, Go packages are actually directories, or folders, in our computers, through which directory structure and file organization are carried out. Go just made a translation of the directory name, called "package". For example, the net package here is actually the net directory, and the http package is actually the http directory, which is also a naming convention in the Go language. The package name is the same as the directory name where the file is located.
Naming of the package
The naming of packages in Go follows the principles of simplicity, lowercase and the same name as the directory where the Go file is located, so that it is easy for us to reference, write, and quickly locate and find.
For example, the http package that comes with Go. All the Go files in this http directory belong to this http package, so when we use the functions and interfaces in the http package, we can import this http package.
Package mainimport "net/http" func main () {http.ListenAndServe ("127.0.0.1 http.ListenAndServe", handler);}
You can see from this example that we are importing net/http, which is called full path in the Go language. Because the http package is in net, net is the top-level package, so the Go compiler must use full-path import to find the http package, which is the same as the directory path of our file system.
Because of the full path, the named package names can be the same as those of other libraries, as long as their full paths are different. Using full-path import also increases the flexibility of package name naming.
For programs developed by individuals or companies, we usually use the domain name as the top-level package name, so that we don't have to worry about duplicating the package names of other developers. For example, if my personal domain name is www.flysnow.org, then my own Go programs use flysnow.org as the top part of the full path. For example, import a toolkit I developed:
Package mainimport "flysnow.org/tools"
What if you don't have your own domain name? You can use Github.com at this time. In the R & D industry, there will be an account in Github. If you don't apply for one quickly. At this point, we can use github.com/ as your top-level path, and no one else will repeat it with you.
Package mainimport "github.com/rujews/tools"
This is how you name it instead of Github.com.
Main package
When you declare the package name of a Go file as main, you tell the Go compiler that this is an executable program, and the Go compiler will try to compile it into a binary executable file.
A main package must contain a main () function, which is not unfamiliar to us. For example, both C and Java have main () function, which is the entrance to a program. Without this function, the program cannot be executed.
In the Go language, both the main package and the main () function must be satisfied before it is compiled into an executable file.
Let's take a look at a Go language version of Hello World to illustrate the main package.
Package mainimport "fmt" func main () {fmt.Println ("Hello")}
Suppose the Go file is called hello.go and is placed in the $GOPATH/src/hello directory, then we execute the go build command in this directory to generate a binary executable file. Under the window system, hello.exe; generates hello under UINX, MAC and Linux. If we execute it in CMD or terminal, we can see the console print:
Hello, the world
The name of the binary executable is the name of the directory where the Go file of the main package is located. Because the hello.go is in the hello directory, the name of the generated executable is hello.
Import package
To use a package, you must import it before you can use it. The Go language provides the import keyword to import a package, which tells the Go compiler where to find the package you want to import, so the imported package must be a full-path package, that is, where the package is located.
Import "fmt"
This means that we have imported the fmt package, which is tantamount to telling the Go compiler that we are going to use the code under this package. What if you want to import multiple packages? The Go language also provides us with import blocks.
Import ("net/http"fmt")
Use a pair of parentheses to include the import block, with each package on a unique line.
For package names with more than one path, when referencing in the code, use the last package name of the full path as the referenced package name, such as net/http, we use http instead of net in the code.
Now that I've imported the packages, where does the Go compiler look for them when compiling? It's time to introduce the environment variables of Go. Go has two very important environment variables GOROOT and GOPATH. These are two environment variables that define the path. GOROOT is the path where Go is installed. For example, / usr/local/go;GOPATH is the developer's personal workspace defined by us, such as / home/flysnow/go.
The compiler will use the two paths we set, plus the relative full path imported by import, to find the package on disk, such as the fmt package we imported, and the compiler will eventually find the location / usr/local/go/fmt.
What is worth understanding is: first, there is priority for package lookup, and the compiler will first search in GOROOT; secondly, GOPATH will stop searching as soon as it is found. If it is not found in the end, a compilation exception is reported.
Remote package import
In the era of the Internet, more and more people use shared code similar to Github. If some Go packages are shared on Github, we also have a way to use them. This is a remote import package, or a network import. Go naturally supports this situation, so we can freely use the Go library on Github to develop programs.
Import "github.com/spf13/cobra"
This kind of import, the premise is that the package is hosted on a distributed version control system, such as Github, Bitbucket, etc., and has the permission of Public, which allows us to access them directly.
When the compiler imports them, it will first search for the package under GOPATH, and if it cannot find it, it will use the go get tool to get it from the version control system (GitHub), and will store the obtained source code in the directory corresponding to URL in the GOPATH directory for compilation.
The go get tool can get dependency packages recursively, and if github.com/spf13/cobra also references other remote packages, the tool can be downloaded as well.
Named Import
We know that after importing the package using the import keyword, we can use the corresponding functions, interfaces, and so on under the package through the package name in the code. What if the package name we imported happens to be duplicated? In response to this situation, the Go language allows us to rename the imported package, which is named import.
Package mainimport ("fmt myfmt" mylib/fmt ") func main () {fmt.Println () myfmt.Println ()}
If it is not renamed, it is not clear to the compiler to distinguish between the two fmt. Renaming is also very simple, when we import, on the left side of the package name, a new package name will be fine.
The Go language stipulates that imported packages must be used, otherwise package compilation errors will occur, which is a very good rule, because it can avoid bloated code and huge programs caused by referencing a lot of useless code. Most of the time, we don't know which packages are used, which are often encountered on C # and Java, so we have to use tools to find files, types, methods, and variables that we don't use and clean them up.
But sometimes, we need to import a package, but do not use it, according to the rules, this is not possible, so the Go language provides us with a blank marker _, as long as we use _ to rename our imported package.
Package mainimport (_ "mylib/fmt")
The init () function of the package
Each package can have as many init () functions as you want, and these init () functions are executed before the main () function. The init () function is usually used to initialize variables, set packages, or other bootstrap tasks that need to be done before the program executes. For example, the purpose of using the _ empty flag to import a package mentioned above is to execute the init () function in the package.
We take the driver of the database as an example. In order to unify the access to the database, Go language uses databases/sql to abstract the operation of a layer of database, which can satisfy us to operate MYSQL, Postgre and other databases. In this way, no matter which driver we use in these databases, the coding operation is the same, and when we want to change the driver, we can change it directly without modifying the specific code.
These database-driven implementations are specific and can be implemented by anyone. Its principle is to define the init () function and register the implemented drivers in the sql package before the program runs, so that we can use it to operate the database.
Package mysqlimport ("database/sql") func init () {sql.Register ("mysql", & MySQLDriver {})}
Because we just want to execute the init () method of the mysql package and do not want to use the package, we need to use _ rename the package name when importing the package to avoid compilation errors.
Import "database/sql" import _ "github.com/go-sql-driver/mysql" db, err: = sql.Open ("mysql", "user:password@/dbname")
Look very concise, the rest of the database operations are using the database/sql standard interface. If we want to change a mysql driver, we only need to change the import, which is flexible and convenient, which is also the convenience of interface-oriented programming.
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.