Go command
The go language comes with a complete set of command manipulation tools that you can view by executing go on the command line:
Figure 1.3 Go command displays detailed information
These commands are very useful for the code we write in peacetime, and then let us know some common commands.
Go Build
This command is used primarily for test compilation. In the package compilation process, if necessary, the package associated with it is compiled.
1. If it's a normal package, like the one we wrote in section 1.2, it doesn't produce any files after you execute go mymath. If you need to generate the appropriate files under $GOPATH/PKG, you will have to perform the go install.
2. If it is the main package, it will generate an executable file in the current directory when you execute the go build. If you need to generate the appropriate files under $gopath/bin, you need to perform the go install or use the Go build-o path/a.exe.
3. If you have multiple files under a project folder, and you only want to compile a file, you can add the file name after the go build, for example, the Go build a.go;go builds command compiles all the went files under the current directory by default.
You can also specify the file name of the compiled output. For example, in the 1.2 section of the Mathapp application, we can specify go build-o astaxie.exe, the default is your package name (not the main package), or the first source file name (main package).
(Note: In fact, the name that is used after "package" in the code in the Go language specification can be different from the folder name package.) The default generated executable file name is the folder name. )
The go build ignores the "_" or "." In the directory. The start of the go file.
If your source code requires different processing for different operating systems, you can name the file according to the different operating system suffixes. For example, there is a program that reads an array, which may have several source files for different operating systems:
Copy Code code as follows:
Array_linux.go array_darwin.go Array_windows.go Array_freebsd.go
The go build will selectively compile files (Linux, Darwin, Windows, Freebsd) that end with the system name. For example, the Linux system will only select Array_linux.go files, other system named suffix files are ignored.
Go clean
This command is used to remove the compiled generated files from the current source package. These documents include
Copy Code code as follows:
_obj/old Object directory, left by makefiles
_test/old Test directory, left by makefiles
_testmain.go old Gotest file, left by makefiles
Test.out old Test record, left by makefiles
Build.out old Test record, left by makefiles
*. [568ao] Object file, left by makefiles
DIR (. exe) is generated by the go build
Dir.test (. exe) is generated by Go test-c
Mainfile (. exe) is generated by the go build mainfile.go
I usually use this command to clear the compiled file, and then GitHub to submit the source code, in the native test when these compile files are related to the system, but for the source code management is not necessary.
Go FMT
Readers who have experience with C + + will know that some people often argue about the K&r style or ANSI style of the code. In go, the code has a standard style. We often write code in ANSI style or other formats that are more appropriate to our own, due to previous habits or other reasons. This will add unnecessary burdens to people reading other people's code, so go enforces code formatting (such as opening curly braces must be at the end of a line), and code that does not follow this format will not compile through , in order to reduce the time wasted on typesetting, the Go tool set provides a go FMT command that helps you format your code files so that you don't need to care about the format when you write code, and you just have to execute go FMT < filename >.go when you're done. Your code has been modified to the standard format, but I usually rarely use this command, because the development tools in general with the save time automatically formatted function, this function in fact, at the bottom is called Go fmt. In the next section I'll talk about two tools, both of which automate the Go FMT feature when saving files.
Copy Code code as follows:
Use the Go FMT command, more often with gofmt, and you need parameter-W, otherwise formatting results will not be written to the file. Gofmt-w src, you can format the entire project.
Go get
This command is used to dynamically obtain remote code packs, currently supported by BitBucket, GitHub, Google code and launchpad. This command is actually divided into two steps internally: The first step is to download the source package, and the second step is to perform the go install. The Go tool of the download source package will automatically invoke different source tools according to the different domain name, the corresponding relation is as follows:
Copy Code code as follows:
BitBucket (Mercurial Git)
GitHub (Git)
Google Code Project Hosting (Git, Mercurial, Subversion)
Launchpad (Bazaar)
So for go to work, you have to make sure that you have the right source management tools installed, and add these commands to your path at the same time. In fact, go-get supports the ability to customize the domain name, specifically see go to help remote.
Go Install
This command is actually divided into two steps internally: The first step is to generate the result file (executable or. a), and the second step is to move the compiled results to $gopath/pkg or $gopath/bin.
Go test
Executing this command automatically reads the file named *_test.go under the source directory, generating and running the test executable file. The output is similar to the information
Copy Code code as follows:
OK Archive/tar 0.011s
FAIL Archive/zip 0.022s
OK compress/gzip 0.033s
...
By default, no parameters are required, and it automatically tests all test files below your source pack, but you can also take the parameters with you, please refer to go help Testflag
Go doc
(1.2rc1 has no go doc instructions, leaving only godoc instructions) Many people say go doesn't need any third-party documents, such as CHM manuals (I've already done a CHM manual) because it has a very powerful documentation tool inside.
How do I see the corresponding package document? For example, BUILTIN package, then execute Go doc builtin if it is an HTTP packet, then run to see the function in a package, then execute Godoc FMT Printf can also see the corresponding code, execute GODOC-SRC FMT Pr intf
Execute godoc-http= on the command line by command: port number such as godoc-http=:8080. Then open the 127.0.0.1:8080 in the browser and you will see a golang.org local copy, through which you can query pkg documents and other content. If you set up Gopath, in the pkg category, not only will the standard package document be listed, but the documentation for all the items in your local gopath will be listed, which is a good choice for users who are often walled.
Other commands
Go also offers a number of other tools, such as the following
Copy Code code as follows:
Go fix is used to fix previous versions of the code to the new version, such as Go1 before the old version of the code was converted to Go1
Go version View the current release
Go ENV view current GO's environment variables
The Go list lists all currently installed package
Go run compile and run go program
Many of these tools are not covered, and users can use the go to help command to get more detailed information.