Go Common commands
Go build
The go build command is mainly used for testing and compiling. During the package compilation process, if necessary, the associated packages will be compiled at the same time.
If it is a normal package, no files will be generated after you execute the go build command.
If the main package is used, an executable file is generated in the current directory after only the go build command is executed. To generate the corresponding exe file under $ GOPATH/bin, Run go install or use go build-o path/a.exe.
If a folder contains multiple files and you only want to compile one of them, you can add the file name after go build, for example, go build. by default, the go build command compiles all go files in the current directory.
You can also specify the name of the compiled output file. For example, you can specify go build-o myapp.exe. The default value is your package name (not the main package) or the name of the first source file (main package ).
Go build ignores the go files starting with "_" or "." in the directory.
If your source code needs to be processed differently for different operating systems, you can name files based on different operating system suffixes. For example, a program that reads arrays may have the following source files for different operating systems:
array_linux.go array_darwin.go array_windows.go array_freebsd.go
Go build will selectively compile the files ending with the system name (Linux, Darwin, Windows, and Freebsd ). For example, in Linux, only the array_linux.go file is selected for compiling, and all the suffix files in other systems are ignored.
Go clean
The go clean command is used to remove the files compiled in the current source code package. These files include
- _ Obj/old object directory, left by Makefiles
- _ Test/old test directory left by Makefiles
- _ Testmain. go old gotest file, left by Makefiles
- Old test record of test. out, left by Makefiles
- Old test records of build. out, left by Makefiles
- *. [568ao] object file, left by Makefiles
- DIR(.exe) is generated by go build.
- DIR.test(.exe) is generated by go test-c.
- MAINFILE(.exe) is generated by go build MAINFILE. go.
Go fmt
The go fmt command is mainly used to help you format written code files.
For example, if we write a test. go file in bad format, we only need to use the fmt go test. go command to help us format our code file. However, this command is rarely used, because our development tools generally have the automatic Formatting Function during storage. The underlying function is actually the go fmt command.
Use the go fmt command. In most cases, use gofmt and the-w parameter. Otherwise, the formatted result will not be written to the file. Gofmt-w src, which can format the entire project.
Go get
The go get command is mainly used to dynamically obtain remote Code packages. Currently, BitBucket, GitHub, Google Code, and Launchpad are supported. This command is actually divided into two steps: the first step is to download the source package, and the second step is to execute go install. The go tool for downloading the source code package automatically calls different source code tools based on different domain names. The relationship is as follows:
BitBucket (Mercurial Git)GitHub (Git)Google Code Project Hosting (Git, Mercurial, Subversion)Launchpad (Bazaar)
To ensure that go get works properly, you must install appropriate source code management tools and add these commands to your PATH. In fact, go get supports the function of customizing domain names. For details, see go help remote.
The go get command can be understood as follows: First, clone the code to the src directory using the source code tool, and then execute go install.
Go install
The go install command is actually divided into two steps: the first step is to generate a result file (Executable File or. the second step moves the compiled result to $ GOPATH/pkg or $ GOPATH/bin.
. Exe file: this file is generally generated by go install with the main function. It has a function entry and can be run directly.
. A application package: generally generated by go files where go install does not contain the main function. It can only be called without function entry.
Go test
The go test command automatically reads the * _ test. go file under the source code directory and generates and runs the executable file for testing. The output information is similar
ok archive/tar 0.011sFAIL archive/zip 0.022sok compress/gzip 0.033s ...
By default, no parameters are required. It will automatically test all the test files in your source package. Of course, you can also include parameters. For details, see go help testflag.
Go doc
The go doc command is actually a powerful document tool.
How can I view the corresponding package documentation? For example, if the builtin package is used, execute go doc builtin. If it is an http package, execute go doc net/http. If you want to view the functions in a certain package, execute godoc fmt Printf; you can also view the corresponding code and execute godoc-src fmt Printf;
Run the godoc-http =: port number in the command line, for example, godoc-http =: 8080. Open 127.0.0.1: 8080 in the browser, and you will see a local copy version of golang.org. With this version, you can query pkg documents and other content. If you set GOPATH, under the pkg category, not only the documents of the standard package are listed, but also the documents of all the projects in your local GOPATH, this is a good choice for users who are frequently restricted.
Other commands
The Go language also provides other useful tools, such as the following.
Go fix is used to fix the code of earlier versions to the new version, for example, if the code of earlier versions of go1 is converted to go1 go version, view the current go version, go env, and check the environment variable of the current go, go list, list all installed packages, go run, compile and run the Go program.