1. A package is a collection of functions and data. Define a package with the pack reserved word. The file name does not need to match the package name. The Convention for package names is to use lowercase characters. Go packages can consist of multiple files, but use the same package <name> this line. Let's define a package called even in the file even.go. Package even← Start Custom packages func even (i int) bool {← Exportable function return I% 2 = = 0}func odd (i int) bool {← Private function return I% 2 = = 1}&nbs p; The name starts with an uppercase letter that is exportable and can be called outside the package, which is discussed later. Now you just need to build this package. Create a directory under $GOPATH and copy Even.go to this directory. % mkdir $GOPATH/src/even← build a level directory% CP even.go $GOPATH/src/even← Copy package file% go build← build it% go install← install it to ~. /pkg next Use this package in the program Myeven.go: Package Mainimport (//import the following packages; " Even "//local Pack even imported here;" FMT "//Call the function in the even package. Func Main () {i: = 5//The syntax for accessing a function in a package is <package>. Function (). Fmt. Printf ("is%deven?%v\n", I,even. Even (i))} % go build myeven.go%./myevenis 5 even? false in Go, when the first letter of the function is capitalized, the function is exported from the package (visible outside the package, or public), so the function name is even. If you modify Myeven.go's section even. Even, use the non-exported function even.odd:fmt. Printf ("is%d even?" %v\n ", I, even.odd (i)) because a private function is used, a compilation error is obtained: Myeven.go:10:cannot refer to unexported name even.odd in summary:? The name of the public function begins with an uppercase letter; The name of the private functionThe word starts with a lowercase letter. The same rule applies to other names defined in the package (new type, global variable). Note that the meaning of "uppercase" is not limited to US ASCII, it is extended to the entire Unicode range. So the capital Greek and the ancient Egyptian language are all possible. &NBSP;&NBSP;2, package name Package name is good, short, concise, well-remembered. According to the rules, the package name is a word in lowercase, and should not be underlined or mixed case. Because everyone may need to enter this name, so as short as possible. Do not consider the conflict package name is the default name of the import, you can give Kaneshige name Eg,import bar "bytes"//rename to bar another rule is that the package name is the root directory name of the code; in Src/pkg/compress/gzip package, as compress/ Gzip import, but the name is gzip, not compress_gzip nor compressgzip. &NBSP;3, test Packages Writing unit tests for packages in Go should be a habit. Writing tests involves testing packages and program go test. Both have good documentation. 4, the common package standard Go code library contains a large number of packages, and most will be installed with the go. Browsing the $GOROOT/src/pkg directory and viewing those packages can be very enlightening. Each package cannot be explained, but these are worth discussing: The FMT package FMT implements formatted I/O functions, similar to C's printf and scanf. The formatted phrase is derived from C. Some phrases (%-sequences) are used in this way:%v values in the default format. When the structure is printed, the plus sign (%+V) increases the field name;% #v The value expression of the go style;%T The value expression of the go style with the type; IO This package provides the raw I/O operation interface. Its main task is to encapsulate the original I/O packages such as the OS package, adding some other correlation so that it has abstract functionality for use on public interfaces. Bufio This package implements buffered I/O. It is encapsulated in IO. Reader and IO. Writer object, creating another object (Reader and writer) that provides buffering while implementing some textual I/O functionality. The Sortsort package provides the original sorting functionality for arrays and user-defined collections. The Strconvstrconv package provides the ability to convert a string to a base data type, or to a string from a base data type. The Osos package provides an interface to the platform-independent operating system functionality. Its design is in the form of Unix. The Syncsync package providesA basic synchronization primitive, such as a mutex. The Flagflag package implements command-line parsing. The Encoding/jsonencoding/json package implements the JSON object defined by encoding and decoding RFC 4627 [5]. Text/template data-driven templates for generating text output, such as HTML. The template is associated to a data structure for parsing. The template content points to the elements of the data structure (usually the fields of the structure or the keys of the map) to control the parsing and determine that a value is displayed. The template scans the structure for parsing, and the "cursor" @ determines the value of the current position in the structure. Net/httpnet/http implements the parsing of HTTP requests, responses, and URLs, and provides extensible HTTP services and basic HTTP clients. The Unsafeunsafe package contains all unsafe operations on the data type in the Go program. This is not usually necessary. The Reflectreflect package implements run-time reflection, allowing the program to manipulate objects through abstract types. Typically used to handle the value of a static type interface{}, and its dynamic type information is resolved through Typeof, and typically returns an object with the type of interface. The OS/EXECOS/EXEC package executes the external command.
Go Language Basics (iv) package