This is a creation in Article, where the information may have evolved or changed.
Order
This article mainly describes the Go Language command line Operation command Details, this article focuses on the go build, go clean, go fmt, go get command, the need for friends can refer to.
Go command
The go language comes with a complete set of command-handling tools that you can view by executing go on the command line.
Go command. png
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.
- If it is a normal package, there is no main function, and when you do go build, it will not produce any files. If you need to generate the appropriate files under $gopath/pkg, go install is required.
- If it is a 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, go to install, or use the Go build-o path/file name you want to generate.
- If you have multiple files under a project folder and you just want to compile a file, you can add the file name after go build, such as go build a.go;
The Go Build command compiles all the go files in the current directory by default. (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:
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 documents include:
_obj/ 旧的object目录,由Makefiles遗留_test/ 旧的test目录,由Makefiles遗留_testmain.go 旧的gotest文件,由Makefiles遗留test.out 旧的test记录,由Makefiles遗留build.out 旧的test记录,由Makefiles遗留*.[568ao] object文件,由Makefiles遗留DIR(.exe) 由go build产生DIR.test(.exe) 由go test -c产生MAINFILE(.exe) 由go build MAINFILE.go产生
Generally use this command to clear the compiled files, and then GitHub to submit the source code, in the native test 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.
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:
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
ok archive/tar 0.011sFAIL archive/zip 0.022sok 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
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:
go fix 用来修复以前老版本的代码到新版本go version 查看go当前的版本go env 查看当前go的环境变量go list 列出当前全部安装的packagego run 编译并运行go程序