go among the language pack

Explanation

Among the language go, all go language program files are organized into groups, each file is referred to as a package. By this arrangement, the code of each package can be used as a multiplexing unit, so as to be used for other projects.

For example, we can go look at them built-net package, probably as follows:

includes a series of relevant documents to .go for the extension in which these directories. Each of which can be individually package import and references, so that developers can import a specific function according to their needs.

go among so-called language package is actually our computer in a folder, a package is a folder.

All .go files, except blank lines and comments should be declared the package they belong in the first row. Each package in a separate directory. You can not put multiple packages into the same directory, nor can split into multiple different directories with a file package. This means that all .go files in the same directory must declare the same package name.

Naming packages

Naming packages go language, follow the simple principles of the directory with the same name in lowercase, and go file is located, so for us to quote, writing and quickly locate find (of course, the name of the package can and directory names are different, but this is not recommended).

Does not require the names of all packages and other packages are different, because when importing packages is to use the full path, it is possible to distinguish between different packages of the same name. Under normal circumstances, after being introduced into the package will use your name as the default package name, but the name can be modified after the import. This feature when you need to import the package of the same name in different directories useful.

For themselves or program developed, we generally use the domain name as a way to top package name, so do not worry and other developers name duplication package.

Github.com such as domain name, then in the development of the program have been github.com as the topmost part of the address on it.

If a kit to be introduced, it may be as follows:

package main 

import "github.com/tools"

Of course, if you use ide, ide so much when you use a package, it will automatically help you contracted introduced in.

main package

Among the go language, main is a special significance. Go language compiler will try to compile this package is the name of the binary executable file. All with Go language compiler executable must have a main package name.

In the main pack, be sure to include the main function, otherwise the program will not create an executable file. main function is the entry procedure, so if the program is not among the main function, the program will not be executed. Directory name of the directory program compiled, would use the statement where the main package code as a binary executable file name.

Let's create a hello packet, to demonstrate content:

package main 

import "fmt"

func main() {
	fmt.Println("hello,world")
}

We cut to the next hello directory through a terminal, then execution go buildwill generate an executable file.

In the windows of which generates executable .exe file in unix or linxu will generate binary executable files.

Import Package

To use a package, you must first import it before you can use, Go language provides a keyword import to import a package, keyword Go tell the compiler where to find the disk package in order to import, so the package must be imported is a bag full path, which is the location of the package is located.

import "fmt"

This means that we import the fmt package, it tells the compiler to go, we want to use this package the following code. If you are importing multiple packages how to do it? Go language also import block us.

import (
	"net/http"
	"fmt"
)

Introducing a pair of blocks included in parentheses, each package on a separate line.

For more than a path of the package name, time referenced in the code, use the full path to the final package name as the package name references, such as net / http, we in the code using http, rather than net.

Import remote package

Go in the process of development, many packages are placed on github them, go language naturally supports the case this remote import package.

E.g:

import "github.com/spf13/cobra"

This import, provided that the package must be hosted on a distributed version control system, such as Github, Bitbucket, etc., and is Public permissions, allows us to access them directly.

When the program is compiled, it will first go to the corresponding package in the local environment which, if not, it will get through go get tools from the version control system (GitHub), download to a specified location for reference compiler to use.

go get the tool can recursively get dependencies, if github.com/spf13/cobra also cited other remote packages, the tool can be downloaded together.

Named import

We know that, in the corresponding function using the import package after introducing keywords, we can use the package through the package name in the code interface. If the package name we import just have duplicate how to do it? In view of this situation, Go language allows us to rename the imported package, which is named after the import.

package main

import (
	"fmt"
	myfmt "mylib/fmt"
)

func main() {
	fmt.Println()
	myfmt.Println()
}

If you do not rename, then for the compiler, it is both fmt area could not tell. Renaming is very simple, when we import, to the left of the package name, from a new package name on it.

Go language stipulates that imported package must be used, otherwise it will pack a compilation error, this is a very good rule, because it eliminates a lot of useless code that we refer resulting code bloat and procedures huge, because a lot of times, we I do not know which package uses, this will often encounter in C and Java, and sometimes we had to use the tool to find files, types, methods and variables we did not use, etc., to clean them out.

But sometimes, we need to import a package, but do not use it, according to the rules, this will not work, for the Go language provides us with a blank identifier _, we only need to use _ to rename the package we import on it.

package main

import (
	_ "mylib/fmt"
)

Function init function

Each packet can have any number of functions init, init these functions are performed before the main function. init function generally used for initializing variables, setting work need guidance or other package prior to program execution. For example, the above we are talking about the need to use _ empty flag to import the purpose of a package that would like to perform in this package init function.

We drive to the database as an example, Go to unify the language on database access, the use of databases / sql abstraction layer of the database operations to meet our operating MYSQL, Postgre and other databases, so no matter which driver we use these databases, coding operation is the same, want to change the driving time, can be directly replaced, without modifying the specific code.

These database-driven to achieve, is the concrete can be achieved by anyone, and its principle is the definition of the init function, before the program is running, the drive to achieve good registration to sql package, so we use it to operate the database using a.

package mysql

import (
	"database/sql"
)

func init() {
	sql.Register("mysql", &MySQLDriver{})
}

We just want to do this because the init method mysql package, do not want to use this package, so when we import this package, you need to use _ to rename the package name, to avoid compilation errors.

import "database/sql"
import _ "github.com/go-sql-driver/mysql"

db, err := sql.Open("mysql", "user:password@/dbname")
Published 10 original articles · won praise 3 · Views 1219

Guess you like

Origin blog.csdn.net/weixin_44568935/article/details/104816656