The previous article documented the installation of the Golang and configuration development environment, and this article will learn about the GO command line commands as well as the usage scenarios.
View available commands
Simply enter in the terminal to go help display all the go commands as well as the corresponding command function introduction, mainly the following:
- Build: Compile packages and dependencies
- Clean: Remove object files
- Doc: A document showing a package or symbol
- ENV: Print Go environment information
- BUG: Start error report
- FIX: Run the Go tool fix
- FMT: Run gofmt for formatting
- Generate: Generate go file from processing source
- Get: Download and install packages and dependencies
- Install: Compile and install packages and dependencies
- List: List Packages
- Run: Compile and run the Go program
- Test: Run Tests
- Tool: Run the tools provided by Go
- Version: Shows the release of Go
- Vet: Run Go tool vet
Commands are used as: go command [args] , in addition, can be used go help <command> to display more help information for the specified command.
When you run go help, you not only print the basic information of these commands, but also give some helpful information about the concepts:
- Mutual invocation of C:go and C
- Buildmode: Description of the build pattern
- filetype: File type
- Gopath:gopath Environment variables
- Environment: Environment variables
- Importpath: Import Path syntax
- Packages: Description of the package list
- Testflag: Test Symbol description
- TestFunc: Test function description
Also use go help <topic> to view the information for these concepts.
Build and Run commands
Just like other statically typed languages, to execute a go program, you need to compile and then execute the resulting executable file. go buildthe command is used to compile the Go program to generate the executable file. But not so the Go program can be compiled to build executable files, to generate executable files, go program needs to meet two conditions:
- The go program needs to belong to the main package
- You must also include the main function in the main package
This means that the Go program's entry is the main main.main function under Main package, example (HELLO.GO):
package mainimport"fmt"func main() { fmt.Println("Hello World!")}
Compile the Hello.go, and then run the executable program:
go run hello.go # 将会生成可执行文件 hello./hello # 运行可执行文件Hello World!
This is the basic use of go build, and if you use go build is not an executable program, but a package, then will not be generated executables.
go runthe command can take the above two steps and execute it in one step (no intermediate files are produced).
go run hello.goHello World!
The above two commands are very common in development.
Additionally the go Clean command can be used to remove the resulting executable program:
go clean # 不加参数,可以删除当前目录下的所有可执行文件go clean sourcefile.go # 会删除对应的可执行文件
FMT and Doc Commands
The go language has a mixed feature, that is, the requirements of the format is very strict, I like this feature, because it can keep the code clear and consistent, compile the combination of development, and go also provides a very powerful tool to format code, it is go fmt sourcefile.go , but usually do not need to call us manually, Various editors can help us automatically complete the formatting.
go doccommand allows us to quickly view the pack documentation file, and the go doc package command will print out the document for the specified package in the terminal.
There is also a command go doc -related command that godoc can be used to launch our own document server:
godoc -http=:8080
Then we can localhost:8080 view the go document with the browser
Get command
This command is also very common, we can use it to download and install a third-party package, using the way:
go get src
Download or update the specified code and dependencies from the specified source, compile and install them, for example, we want to use Beego to develop Web applications, we first need to get Beego:
go get github.com/astaxie/beego
This command will automatically download the installation Beego and its dependencies, and then we can use it in the following ways:
package mainimport"github.com/astaxie/beego" # 这里需要使用 src 下的完整路径func main() { beego.Run()}
Install command
To compile and install the Go program, we can compare it to the build command:
|
Install |
Build |
| The resulting executable file path |
Under the Bin directory in the working directory |
Under current directory |
| The name of the executable file |
The same name as the directory where the source |
The default is the same name as the source program, you can use the-o option to specify |
| Depend on |
Place dependent packages under the Pkg folder in the working directory |
- |
Test command
As the name implies, the command used to run the test, which is in the package unit. There are certain rules to follow:
- The test source file is named with "_test.go" as the suffix
- Test source file contains several test functions of the source file
- The test function is usually prefixed with the name "Test" and has a type of "testing". T "parameter.
Other commands
Other commands are not used frequently, and are not described here, and are used directly to view the commands when they are really used go help command .
Go language Learning-common commands