Go language command line operation commands
Go command
Go comes with a complete set of command manipulation tools, which you can view by executing go from the command line:
Figure 1.3 Go command shows the details
These commands are very useful for the code we usually write, so let's look at some common commands.
go build
This command is mainly used to test compilation. During the compilation of a package, the packages associated with it are compiled at the same time, if necessary.
1. If it is a normal package, like the mymath package we wrote in Section 1.2, it does not produce any files when you execute go build. If you need to generate the appropriate file under $GOPATH/pkg, you will have to execute go install.
2. If it is the main package, when you execute go build, it will generate an executable file in the current directory. If you need to generate the appropriate file under $GOPATH/bin, you need to go install, or use the go build -o path /a.exe.
3. If there are multiple files in a project folder, and you only want to compile a file, you can add the file name after go build, for example, go build a.geo; By default, the go build command compiles all go files in the current directory.
You can also specify the file name of the compiled output. For example, in the mathapp application in Section 1.2, we can specify go build-o astaxie.exe, which by default is either the name of your package (not the main package) or the name of the first source file (the main package).
Note: In fact, the package name in the Go specification refers to the name used after "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 directories that start with "_" or ". The start of the go file.
If your source code needs different processing for different operating systems, then you can name the files according to different operating system suffixes. For example, a program that reads an array may have the following source files for different operating systems:
array_linux.go array_darwin.go array_windows.go array_freebsd.go
go build selectively builds files ending in the system name (Linux, Darwin, Windows, Freebsd). For example, only the array_linux.go file is selected for the following compilation in Linux, and all other system naming suffix files are ignored.
go clean
This command is used to remove the compiled files from the current source package. These documents include
_obj/ old object directory, left over by Makefiles
_test/ Old test directory, left over by Makefiles
_testmain.go The old gotest file, left behind by Makefiles
Test. out Indicates the old test record, which is left by the Makefiles
build.out Old test records, left over by Makefiles
*.[568ao] object file, left by Makefiles
DIR(.exe) generated by go build DIR.test(.exe) generated by go test -c MAINFILE(.exe) generated by go build mainfile.go
I usually use this command to clear the compilation file, and then github submit the source code, in the local test when these compilation files are relevant to the system, but not necessary for source management.
go fmt
Readers with C/C++ experience will know that some people often argue about whether the code should be K&R style or ANSI style. In go, the code has a standard style. Because of some custom or other reason we often write code in ANSI style or other more suitable format, which will put unnecessary burden on people to read other people's code, so go enforces the code format (for example, the open bracket must be placed at the end of the line), and code that does not conform to this format will not compile. In order to reduce the waste of time in typesetting, go tool set provides a go fmt command which can help you format your written code file, so that you do not need to care about the format when writing code, you only need to execute go fmt < file name >.go after writing, your code will be modified into a standard format. But I usually rarely use this command, because the development tools generally have a save time automatic formatting function, this function is actually at the bottom is to call go fmt. In the next section, I'll cover two tools that come with automatic go fmt functionality when saving files.
Use the gofmt command, more often gofmt, and need the parameter -w, otherwise the formatting result will not be written to the file. gofmt -w src to format the entire project.
go get
This command is used to dynamically retrieve remote Code packages, currently supported by BitBucket, GitHub, Google Code, and Launchpad. Internally, 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 that downloads the source package will automatically call different source tools according to different domain names, and 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, you need to make sure that you have the right source management tools installed and that you add these commands to your PATH as well. In fact, go get supports the function of custom domain name, see go help remote for details.
go install
Internally, this command is actually divided into two steps: the first step generates the result file (executable file or.a package), and the second step moves the compiled result to $GOPATH/pkg or $GOPATH/bin.
go test
Run this command to automatically read the source directory named *_test.go file, generate and run the test executable file. The output information is similar
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
By default, it does not need any parameters, it will automatically test all the test files under your source package, of course, you can also bring parameters, details please refer to go help testflag
go doc
(After 1.2rc1 there is no godoc instruction, leaving only godoc instructions) Many people say that go does not need any third-party documentation, such as the chm manual (actually I have made one, the chm manual), because it has a very powerful documentation tool inside.
How do I view the documentation for the corresponding package? For example, if builtin is an http package, then godoc builtin is executed. If it is an http package, then godoc net/ HTTP is executed to view the functions in one of the packages. If godoc fmt Printf is executed, you can also view the corresponding code. Run godoc -src fmt Printf
Run the godoc-http =: port number command on the CLI, for example, godoc-http =:8080. Then open 127.0.0.1:8080 in your browser and you will see a local copy of golang.org from which you can query pkg documents and other content. If you set up GOPATH, under the pkg category, not only the documentation of the standard package will be listed, but also the relevant documentation of all the projects in your local GOPATH, which is a good choice for users who are often walled.
Other commands
go also provides many other tools, such as the ones below
go fix is used to fix the old version of the code to the new version, for example, the old version of the code before go1 is converted to go1
go version Displays the current version of go
go env Displays the current go environment variable
go list lists all currently installed packages
go run compiles and runs the Go program
There are many parameters that are not described in the preceding tools. You can run the go help command to obtain more detailed help information.
- Golang development command line flag package usage
- One second understand Go to write command line tool code
- Explain how to create a command line tool in Go
- In Go language, the flag package is used to parse parameters on the command line
- go language method of executing the command line in windows
- Go language Method of executing system command line commands
- golang implements the help function of command line programs
Related article
-
Understand the Go language io.Copy function
This article mainly introduces the Go language io.Copy function use examples detailed explanation, need friends can use for reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise2023-07-07 -
Here's an in-depth look at spin locks in Golang
Spin lock is a kind of busy waiting lock, when a thread tries to obtain a lock that has been held by another thread, the thread will continue to check the state of the lock (that is, "spin"), until the lock is released after the ownership, let's take a closer look at the specific operation of spin lock2024-01-01 -
golang method for downloading multiple files concurrently
Today, Xiaobian will share a golang and download multiple files for you, which has a good reference value, and I hope to help you. Let's take a look2019-07-07 -
How to solve the Redis storage dependency problem in Go unit testing
In writing unit tests, in addition to MySQL this external storage dependency,Redis should be another of the most common external storage dependency, this article will explain how to solve Redis external dependency, the article through the code example introduced very detailed, need friends can refer to2023-08-08 -
In-depth understanding of structs in the Go language
This article mainly introduces the in-depth understanding of Go language structure, including the definition of structure, the declaration of structure variables, the use of structure, structure association function, new, combination, etc., the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, need friends to learn together with the small series2023-11-11 -
Text explains the difference between Golang Goroutine and thread
goroutine is a lightweight thread implementation in the Go language, managed by the Go runtime, using each go keyword will open a new coroutine, today through this article to introduce the difference between Golang Goroutine and threads. Interested friends take a look2022-03-03 -
The gin framework in Golang binds two methods for parsing json data
This article introduces Golang's gin framework to receive json data and analyze the two methods, the article through the code examples introduced very detailed, to everyone's study or work has a certain help, need friends can refer to2023-12-12 -
golang how to remove source path information in binary files
This article mainly introduces golang how to delete the source code path information in the binary file, has a good reference value, I hope to help you. Let's take a look2021-04-04 -
Go code specification error handling example experience summary
This article mainly introduces the Go code specification error handling example practical experience summary, friends in need can use for reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise2022-08-08 -
Golang standard library unsafe source code interpretation
This article mainly introduces the Golang standard library unsafe source code interpretation, friends in need can draw on the reference, hope to be helpful, I wish you a lot of progress, early promotion and pay rise2022-08-08
Latest comments