Package - Basic concepts, custom packages, create a package, the export package identifier

1, the basic concept

Go language is to use the source code packages to organize, and manage the namespace. Any source code files must belong to a package. The first line of code valid source file must be a package pacakgeName statement, a statement of where their package by the statement.

1) concept

Go language pack by means of the organization of the directory tree, common name is the source directory where the file name of the package, although Go is not mandatory package name must be their directory name the same name, but it is recommended that the package name and directory of the same name, so structure more clearly.

Deep packet can be defined in the directory, it is not defined in the packet including the directory path, but the reference packet is a full path generally by reference. For example, in $ GOPATH / src / a / b / define a package c. C source in the package is simply declared package c, but not declared as package a / b / c, but in the "import" c pack, need to take the path import "a / b / c".

Package idiom:

  • General package is lowercase, use a short name.
  • General to package and directory of the same name is located.

2) package references

Standard source packet located $ GOROOT / src / below, the standard package can be directly invoked. And third-party packages from source package definition must put $ GOPATH / src directory can be referenced.

REFERENCE packet writing, there are two paths, one is the full path, the other is a relative path.

Absolute path of the package is the "$ GOROOT / src or $ GOPATH / src" full path to the source of bread.

3) reference packet format

Standard reference: import "fmt"

At this time, it can be "fmt." Prefix reference element may be derived within the package, which is a commonly used method.

Alias ​​reference: import F "fmt"

At this time corresponds to the package from the alias fmt F, substituting "F." instead of the standard "fmt." Fmt prefix reference element may be derived encapsulated.

Omit way:. Import "fmt"

At this time, the package is equivalent to fmt namespace namespace incorporated directly into the current program, elements may be derived using the prefix can not package fmt "fmt.", Direct reference.

Only perform initialization packet init function: import _ "fmt"

Using a standard format references package, but did not use the package code, the compiler will complain. If the package includes initialization function init, through import _ "packageName" This reference package, perform the initialization function only package, even if no package initialization function init, it will not lead to a compiler error.

note:

  • A packet can have multiple functions init, the entire package is loaded init function is executed, it does not guarantee the order of execution, it is not recommended in the init function into a plurality of packets, the required logic function to initialize placed inside an init .
  • Package can not appear circular references. Such as a package bag references b, b references bag package c, such that c and packet references packet a, the compiler can not.
  • Duplicate references packet is allowed. Such as a package and the package references c packet b, packet b and c are packet references package d. This scenario corresponds to the referenced d repeated, this situation is allowed, and the compiler guarantees d Go init function is executed only once.

init () function has the following characteristics:

  • Each source can use an init () function.
  • init () function (before the function executes main ()) is called automatically before the program execution.
  • Calling sequence for the reference main () of the package, depth-first order to the initialization.

For example, assume that such a package reference relationship: main → A → B → C, then the packet init () function call order:

C.init→B.init→A.init→main

Description:

  • A plurality of the same package init () function call to the unpredictable order.
  • init () function can not be called by other functions.

 

4) Package load

Go initialization packet has the following characteristics:

  • Package initialization program from the main function reference packet start, step by step to find references to the package, the package until you find no references to other packages, to generate a final package referenced directed acyclic graph.
  • Go compiler will have converted to the acyclic graph is a tree, and then start up layer by layer from the leaf packet node initializes the tree.
  • Single packet initialization process first initializes constants and global variables, the last execution of the package init function (if any).

 

2, custom package

We create a custom package best to put GOPATH src directory (or a subdirectory src GOPATH), if the package is only part of an application, can be placed directly under the subdirectory of the application, but if we hope that this package can be shared by other applications, it should be placed under the src directory GOPATH each package on a single directory, if two different packages in the same directory, name collisions occur compilation error.

Convention as source code, should be placed in a package with the same name folder. The same package can have any number of files, the file name and there is no provision (but subsequent name must be .go), where we assume the file name of the package name is .go (if a package has multiple .go files, where there will be a .go same file name and the package name).

If we want to create a new package in a bag, for example, created the following package my_package two new packages pkg1 and pkg2, you can do so: in aGoPath / src / my_package build two subdirectories, such as aGoPath / src / my_package / pkg1 and aGoPath / src / my_package / pkg2, corresponding package file is aGoPath / src / my_package / pkg1 / pkg1.go and aGoPath / src / my_package / pkg2 / pkg2.go.
Thereafter, if you want to import pkg2, using import my_package / pkg2 can. Go language standard library source tree is one such structure. Of course, my_package directory can have its own package, such as aGoPath / src / my_package / my_package.go file.

 

3, create a package

Package requires that all documents in the same directory as the first line of code is added to mark the file belongs package:

package package name

Package following characteristics:

  • Peer files in a directory under the ownership of a package.
  • Package name may differ in its directory name.
  • Package Package named main entrance package for the application, compile the source code is not the main package, will not compile output executable file.

Each package generally defined access under a different name space for each identifier in its internal. Each name space associated with a particular package, let's give types, functions, and so choose a short and clear name, to avoid and reduce conflicts in other parts of the name when we use them.

Each package can also be seen whether the lead out and achieved by controlling the characteristics of the package to the package name. Embodied visibility and hides API package by limiting the package member, the package will allow the interior defenders outer cladding without affecting the user adjusting their implementation. By limiting the visibility of the encapsulated variables you can also force the user to access and update certain internal variable function, which can ensure consistency and concurrency constraint mutual exclusion internal variables.

 

4, the package identifier derived

In the Go language, if you want to export another quote identifiers when a package identifier (such as types, variables, constants, etc.), you must first be referenced in a bag, the first letter capitalized will be exported to the identifier can make reference to these identifiers can visit the.

Being derived structure or interface, field or method if they first letter is capitalized, the external fields and methods can be accessed.

 

Guess you like

Origin www.cnblogs.com/ACGame/p/11986348.html