This is a creation in Article, where the information may have evolved or changed. Date: July 30, 2014
1. Definition: A collection of functions and data at the time of the package. Define a package with the Package keyword, the file name does not need to be the same as the packet name, the package name convention uses lowercase characters, the go package can consist of multiple files, but the same package<name> is required, and the method in the package is called: <PACKAG> Fuction ()
Right now Create a new file Even.go, the file contents of Even.go are as follows: Package even//determine if it is even func even (i int) bool {return I% 2 = = 0}//Determine if it is odd func odd (i int) bool {return I % 2 = = 1}
It defines two functions, one is even, one is Odd,even, the first letter is odd, the even function can be accessed outside the package, and the odd function is not, which is the convention of the Go language-the name of the public function begins with a capital letter, and the name of the private function begins with a lowercase letter. The same rules apply to new types defined in packages, global variables, and so on, and the meaning of "uppercase" is not limited to us ASCII, it is extended to all uppercase and lowercase alphabets (Latin, Greek, Slavic, Armenian and Egyptian prose).
build the even package:Print the Gopath path, Note that the Gopath path is set when the go locale is installed$ echo $GOPATH e:\programfiles\go\go\goimportpkg//Create a new folder under Gopath/src/even$ mkdir $GOPATH/src/even// Copy the Even.go file to the new directory under the $ CP even.go $GOPATH/src/even//jump to the new catalog CD $GOPATH/src/even//print the current directory information $ pwd/e/programfiles/go/go/ goimportpkg/src/even//compiling Even.gogo bulid//Install even pack go install
After performing the above steps, you can find a file in the $gopath/pkg directory Even.a, open with sublime view: 213c 6172 6368 3e0a 5f5f 2e53 594d 44454620 2020 2020 2020 3020 2020 2 020 20202020 2020 3020 2020 2020 3020 2020 20203634 3420 2020 2020 3133 3620 2020 20202020 600a 54e0 0100 0022 222e 4576 6 56e0044 e001 0000 676f 2e73 7472 696e 672e2266 756e 6328 696e 7429 2062 6f6f 6c220044 e001 0000 7479 7065 2e66 756e 6328. ....
using the even package:Package Mainimport ("even"//import we just customized even pack "FMT"//go built-in package) Func main () {i: = 5 fmt. Printf ("is%d even?" %v\n ", I, even. Even (i))//print is 5 even? False//fmt. Printf ("is%d even?" %v\n ", I, even.odd (i))//error message cannot refer to unexported name even.odd}
2, Package name overlay: Go when importing the package, you can use the import <othername>The Packagenaem method overrides the original package name, and the function in the package can only be called with the new name after overwriting.
3, package of documents: Each package should have a package comment, a comment block in front of 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. Each defined (and exported) function in a package should have a small paragraph of text describing the function's behavior.
4. Unit testing it should be a habit to write unit tests for packages in go. Writing tests involves testing packages and program go test. The test file is also in the package directory, is named *_test.go, these test files and other go files are the same, but go test will only execute the testing function, each test function has the same identity, the name starts with the test, the definition is as follows: 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 fail (), Failnow, Log, fatal and other functions. Example: Create a new Even_test.go file (the name of the unit test file in Go is agreed to xxx_test.go), and the file is in the same directory as the above Even.go file, with the following contents: Package even import "testing" Func Testeven (t *testing. T) { if! Even (2){T.Log ("2 should be even!") T.fail ()}}
Using the package even definition pack, the test uses the same namespace as the package being tested, which is not only for convenience, but also allows testing of non-exported functions and structures (private functions under the same package can be accessed directly).
Import the testing package, define the test function Testeven, and write some test rules in the function body.
At this point: Execute the Go test instructions to pass.
Modify the contents of the Even_test.go file and execute the Go test package even import "testing" func Testeven (t *testing) again. T) { if even (2){//Removed! T.Log ("2 should be even!") T.fail ()}} can thus be used to verify that the syntax rules for the functions in the package are correct.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.