This is a creation in Article, where the information may have evolved or changed.
A package is a collection of functions and data. Define a package with the bundle keyword. 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.
package evenfunc Even(i int) bool { return i%2==0}func odd(i int) bool { return i%2==1}
The name starts with an uppercase letter that is exportable and can be called outside the package.
The name of a common function begins with an uppercase letter.
The name of the private function begins with a lowercase letter.
package mainimport ( //导入下面的包 "event" //本地包even在这里导入 "fmt")func main(){ i := 5 fmt.Printf("Is %d event? %v\n", i,event.Even(i))// 调用even包中的函数。访问一个包中的函数的语法是<package>.Function()。}
Go program naming rules: The package name convention starts with a lowercase letter, the method name is best used camel name, avoid the use of underscores, the method name should be concise and clear.
Documentation for the package
Each package should have a package comment, a comment block before the packages. For multi-file packages, package annotations only need to appear in front of a file, any file can be. Package annotations should describe the package and provide overall information about the package. This will appear on the Go Doc generated page about the package, and the relevant details will be displayed.
Examples of official RegExp packages:
/* The regexp package implements a simple library for regular expressions. The syntax of the regular expressions accepted is: regexp: concatenation '|' concatenation */ package regexp
Each function defined (and exported) should have a short text describing the function's behavior.
Examples of FMT packages:
// Printf formats according to a format specifier and writes to standard // output. It returns the number of bytes written and any write error // encountered. func Printf(format string, a ...interface) (n int, err error)
Test Package
Writing unit tests for a package in go should be a habit. Writing tests involves testing packages and program go test. Both have good documentation.
The Go test program invokes all of the test functions.
The even package does not define any test functions and executes go test, which is:
% go test? even [no test files]
Define a test in the test file to fix this. The test file is also in the package directory and is named *_rest.go. These test files are the same as the other files in the Go program, but go test executes only the testing functions. Each test function has the same label, and his name begins with test:
func TestXxx(t *testing.T)
When writing tests, you need to tell the go test whether the test failed or succeeded. The test succeeds and returns directly. When the test fails, it can be marked with the following function.
func (t *T) Fail() // Fail标记测试函数失败,但仍然继续执行。func (t *T) FailNow() // FailNow标记测试函数失败,并且中断其执行。当前文件中的其余的测试将被跳过,然后执行下一个文件中的测试。func (t *T) Log(args ...interface{}) // Log 用默认格式对其参数进行格式化,与 Print() 类似,并且记录文本到错误日志。func (t *T) Fatal(args ...interface{}) // Fatal 等价于 Log() 后跟随 FailNow()。
Event_test.go
package even // 测试使用与被测试的包使用相同的名称空间import "testing"func TestEven(t *testing.T) { // 定义测试函数 if ! Even(2) { t.Log("should be even!") t.Fail() }}
Common packages
The standard go code base 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.
- 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 like this:
%v
The default format value. When the structure is printed, the plus sign (%+V) increases the field name; % #v
Go-style value expression;
%T
A value expression with a type of Go style;
- Io
This package provides the original 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.
- Sort
The sort package provides the original sorting functionality for arrays and user-defined collections.
- StrConv
The StrConv package provides the ability to convert a string to a base data type, or to a string from a base data type.
- Os
The OS package provides an interface to the platform-independent operating system functionality. Its design is in the form of Unix.
- Sync
The sync package provides a basic synchronization primitive, such as a mutex.
- Flag
The flag package implements command-line parsing. See "Command-Line arguments" on page 92nd.
- Encoding/json
The Encoding/json package implements the JSON object defined by encoding and decoding RFC 4627 [2].
- Html/template
A data-driven template 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/http
Net/http implements the parsing of HTTP requests, responses, and URLs, and provides extensible HTTP services and basic HTTP clients.
- Unsafe
The unsafe package contains all unsafe operations on the data type in the Go program. This is not usually necessary.
- Reflect
The reflect 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.
- Os/exec
The OS/EXEC package executes the external command.