Compile and run a simple program. First select a package path (we will use github.com/user/hello) and create the corresponding package directory in your workspace:
Create a file named hello. go. It was created above and skipped here.
If you are using a source code management system, it is now a good time to initialize a repository, add files, and submit your first changes. Again, this step is optional: you do not need to use source code management to write code.
6. first libraryMkdir $ GOPATH/src/github.com/user/stringutil
Next, create a file named reverse. go under the directory with the following content:
// Package stringutil contains utility functions for working with strings.package stringutil// Reverse returns its argument string reversed rune-wise left to right.func Reverse(s string) string {r := []rune(s)for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {r[i], r[j] = r[j], r[i]}return string(r)}Compile the test package using go build
$ go build github.com/user/stringutil
If the source code package directory is in the current location, you only need:
go build
The above operation does not generate an output file. You must use go install to output the package and object to the pkg directory of the job.
After the stringutil package is created, modify the original hello. go and use the stringutil package:
package mainimport ("fmt""github.com/user/stringutil")func main() {fmt.Printf(stringutil.Reverse("\n !oG ,olleH"))}Whether using the go installation package or binary files, all related dependencies are automatically installed. So when you install the hello program:
$ go install github.com/user/hello
The corresponding stringutil package is automatically installed.
Run the new hello program and you can see that the message has been reversed.
# helloHello, Go!
After completing the preceding operations, the workspace should be:
├── bin│ └── hello # command executable├── pkg│ └── linux_amd64 # this will reflect your OS and architecture│ └── github.com│ └── user│ └── stringutil.a # package object└── src└── github.com└── user├── hello│ └── hello.go # command source└── stringutil└── reverse.go # package source
Note: go install puts the library file stringutil. a under pkg/linux_amd64 (the directory structure is the same as the source code structure ). In this way, the go command can directly find the corresponding package object to avoid unnecessary repeated compilation. Linux_amd64 is used for cross-compiling Based on the operating system and your system architecture.
All the Go executable programs are linked together in static mode. Therefore, related package objects (Libraries) are not required during runtime ).
7. Package commandsAll Go source code starts with the following statement:
package name
The name is the default package reference name. All files in a package must use the same package name and the executable command must be main.
All package names in a binary file do not need to be unique, but the reference path must be unique.
8. TestGo comes with a lightweight testing framework consisting of go test and testing packages.
You can create xx_test.go to write a test, which contains several TestXXX functions. The test framework will automatically execute these functions. If the function contains tError or t. Fail, the corresponding test will be judged as a failure.
Add a test file for stringutil $ GOPATH/src/github.com/user/stringutil/reverse_test.go, containing the following content:
Package stringutilimport "testing" func TestReverse (t * testing. t) {cases: = [] struct {in, want string} {"Hello, world", "dlrow, olleH" },{ "Hello, world", ", olleH "},{" "," "},}for _, c: = range cases {got: = Reverse (c. in) if got! = C. want {t. Errorf ("Reverse (% q) = % q, want % q", c. in, got, c. want )}}}# Test with go test
# go test github.com/user/stringutilok github.com/user/stringutil 0.002s
# Similarly, you can ignore the path in the package folder and directly execute go test
[root@zabbix stringutil]# go testPASSok github.com/user/stringutil 0.002s
9. Remote packageThe Reference Path of the package describes how to obtain the source code of the package through the version control system. The go tool automatically obtains the package file from the remote code repository through the reference path. For example, the examples used in this article are stored in github.com/golang/example. Go can directly obtain, generate, and install the corresponding package through the url of the package code repository.
[root@zabbix ~]# go get github.com/golang/example/hello[root@zabbix ~]# $GOPATH/bin/helloHello, Go examples!
If no corresponding package exists in the workspace, go places the corresponding package in the workspace specified by the GOPATH environment variable. (If the package already exists, go skips the code and runs go install directly)
After the preceding go get command is executed, the following result is displayed in the workspace Folder:
The hello command on github depends on the stringutil library in the same repository. hello. go imports data using the same path. Therefore, the go get command can directly find and install the corresponding dependent package.
import "github.com/golang/example/stringutil"
# It is best to share the Go package in this way.
# Note: For more information about the go tool remote package, refer to: https://golang.org/cmd/go/#hdr-Remote_import_paths
10. Official AddressGo efficient programming: https://golang.org/doc/effective_go.html
Example of Go programming: https://tour.golang.org/welcome/
Address: http://www.linuxprobe.com/set-go-env.html