This is a creation in Article, where the information may have evolved or changed.
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 to display detailed information
These commands are very useful for the code that we normally write, so let's look at some common commands.
Go Build
This command is primarily used for test compilation. During the compilation of a package, the package associated with it is compiled, if necessary.
1. If it is a normal package, just like the MyMath package we wrote in section 1.2, when you execute go build, it will not produce any files. If you need to generate the appropriate files under $gopath/pkg, go install is required.
2. If it is a main package, when you execute go build, it will generate an executable file in the current directory. If you need to generate the appropriate files under $gopath/bin, go to install, or use the Go build-o path/a.exe.
3. If you have multiple files under a project folder and you just want to compile a file, you can add a filename after the go build, such as the Go Build a.go;go build command compiles all the go files in the current directory by default.
You can also specify the file name of the compiled output. For example, in section 1.2 of the Mathapp application, we can specify go build-o astaxie.exe, which is either your package name (not the main one) or the file name (main package) of the first source file.
(Note: In fact, the package name is used in the Go Language specification for the name of the "package" in the code, which can be different from the folder name.) The default generated executable file name is the folder name. )
Go build Ignores "_" or "." In the directory. Start with the go file.
If your source code requires different processing for different operating systems, you can name the files 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 CodeThe code is as follows:
Array_linux.go array_darwin.go Array_windows.go Array_freebsd.go
The go build will selectively compile files that end with the system name (Linux, Darwin, Windows, Freebsd). For example, the following compilation of Linux system only selects the Array_linux.go file, and all other system naming suffix files are ignored.
Go clean
This command is used to remove the generated files from the current source package. These files include
Copy CodeThe code is 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 GO build
Dir.test (. exe) generated by Go test-c
Mainfile (. exe) generated by GO build mainfile.go
I generally use this command to clear the compilation 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 management is not necessary.
Go FMT
Readers who have had a C + + experience will know that some people often argue that the code takes K&R style or ANSI style. In go, the code has a standard style. Because of some of the habits or other reasons we used to write code in ANSI style or other more appropriate format, this will add an unnecessary burden to people when they read someone else's code, so go enforces the code format (for example, the opening brace must be placed at the end of the line), and code that does not follow this format will not compile , in order to reduce wasted time on typesetting, the Go toolset provides a go FMT command that can help you format your written code files so that you don't need to care about the format when you write the code, you only need to execute go FMT < filename >.go After you finish writing, Your code has been modified to the standard format, but I rarely use this command, because the development tool usually has a save time automatic formatting function, which is actually called go fmt at the bottom of the function. In the next section I'll cover two tools, both of which have their own automated go FMT when saving files.
Copy CodeThe code is as follows:
Use the Go FMT command, more often with gofmt, and require the parameter-W, otherwise the 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 packages, currently supported by BitBucket, GitHub, Google code, and Launchpad. This command is actually divided into two steps inside: The first step is to download the source package, the second step is to execute go install. Download the source package of the Go tool will automatically according to different domain names to invoke different source tools, the corresponding relationship is as follows:
Copy CodeThe code is as follows:
BitBucket (Mercurial Git)
GitHub (Git)
Google Code Project Hosting (Git, Mercurial, Subversion)
Launchpad (Bazaar)
so in order for go get to work properly, you have to make sure that you have the appropriate 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, see go help remote for details.
Go Install
This command is actually divided internally into two steps: 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
By executing this command, the file named *_test.go is automatically read from the source directory, and the executable file for the test is generated and run. The output information is similar
Copy CodeThe code is 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 the test files under your source package, but you can also take the parameters, for more information, please refer to go help Testflag
Go doc
(1.2rc1 does not have go doc instructions, leaving only godoc instructions) Many people say that go does not need any third-party documents, such as the CHM manual (in fact, I have done one, the CHM manual), because it has a very powerful document tool inside.
How do I view the documentation for the package? For example BUILTIN package, then execute Go doc builtin if it is an HTTP packet, then execute GO Doc net/http see the function inside a package, then execute Godoc fmt Printf can also view the corresponding code, execute GODOC-SRC FMT Pr intf
Execute godoc-http= on command line via command: port number such as godoc-http=:8080. Then open 127.0.0.1:8080 in the browser, you will see a golang.org local copy version, through which you can query the PKG document and other content. If you set up Gopath, under the PKG category, not only will the standard package document be listed, but also the documentation for all the items in your local gopath, which is a good choice for users who are often wall-related.
Other commands
Go also offers a number of other tools, such as the following
Copy CodeThe code is as follows:
Go fix is used to fix old version of code to new version, such as Go1 before the old version of the code into GO1
Go version View the current release of Go
Go env View the current GO environment variables
Go list lists all currently installed package
Go run to compile and run the Go program
Many of these tools are not covered, and users can use the Go Help command for more detailed information.