Home  >  Article  >  Backend Development  >  What are the commonly used commands in go language?

What are the commonly used commands in go language?

PHPz
PHPzOriginal
2020-12-16 16:23:015396browse

Commonly used commands in go language are: 1. go build; 2. go clean; 3. go fmt; 4. go get; 5. go install; 6. go test; 7. go doc; 8. go fix; 9. go version; 10. go list.

What are the commonly used commands in go language?

The environment of this article: windows10 system, Go 1.11.2, this article is applicable to all brands of computers.

Introduction:

Commonly used commands in go language are: 1. go build; 2. go clean; 3. go fmt; 4. go get; 5. go install; 6. go test; 7. go doc; 8. go fix; 9. go version; 10. go list.

(Learning video sharing: Programming video)

Commonly used commands are as follows:

1. go build

go build command is mainly Is used for testing compilation. During the compilation process of the package, if necessary, the packages associated with it will be compiled at the same time. If it is a normal package, no files will be generated when you execute the go build command. If it is the main package, after executing only the go build command, an executable file will be generated in the current directory. If you need to generate the corresponding exe file under $GOPATH/bin, you need to execute go install or use go build -o path/a.exe.

If there are multiple files in a folder and you only want to compile one of them, you can add the file name after go build, such as go build a.go; the go build command will compile by default All go files in the current directory. You can also specify the file name of the compiled output. For example, we can specify go build -o myapp.exe. The default is your package name (non-main package), or the file name of the first source file (main package). go build will ignore go files starting with "_" or "." in the directory.

If your source code requires different processing for different operating systems, then you can name the files according to different operating system suffixes. For example, there is a program that reads arrays. It may have the following source files for different operating systems:

array_linux.go
array_darwin.go
array_windows.go
array_freebsd.go

When go builds, files ending with the system name will be selectively compiled (Linux , Darwin, Windows, Freebsd). For example, under Linux system, only the array_linux.go file will be selected for compilation, and all files with suffix names on other systems will be ignored.

2. go clean

The go clean command is used to remove files compiled and generated 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
test.out Old test record, left by Makefiles
build .out old test record, left by Makefiles
*.[568ao] object file, left by Makefiles
DIR(.exe) generated by go build
DIR.test(.exe) generated by go test - c generates
MAINFILE(.exe) generated by go build MAINFILE.go

3. go fmt

The go fmt command is mainly used to help you format the code files you have written. .

For example, if we write a badly formatted test.go file, we only need to use the fmt go test.go command to let go help us format our code file. But we rarely use this command, because our development tools generally have an automatic formatting function when saving. The bottom layer of this function is actually calling the go fmt command.

Use the go fmt command, more often gofmt, and the parameter -w is required, otherwise the formatting result will not be written to the file. gofmt -w src, you can format the entire project.

4. 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 internally: the first step is to download the source code package, and the second step is to execute go install. The go tool that downloads the source code package will automatically call different source code tools according to different domain names. 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 must ensure that the appropriate source code management tools are installed 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 essentially be understood as: first clone the code to the src directory through the source code tool, and then execute go install.

5. go install

The go install command is actually divided into two steps internally: the first step is to generate the result file (executable file or .a package), and the second step will Move the compiled results to G O P A T H / p k g or GOPATH/pkg or GOPATH/pkg or GOPATH/bin.

.exe file: Generally generated by go install and go file with main function. It has function entry and can be run directly.

.a application package: Generally generated by go install and go file that does not contain the main function. There is no function entry and can only be called.

6. go test

The go test command will automatically read the file named *_test.go under the source code directory, generate and run the executable file for testing. The output information is similar to

ok archive/tar 0.011s FAIL archive/zip 0.022s ok compress/gzip
0.033s …

默认的情况下,不需要任何的参数,它会自动把你源码包下面所有test文件测试完毕,当然你也可以带上参数,详情请参考go help testflag

7、go doc

go doc 命令其实就是一个很强大的文档工具。

如何查看相应package的文档呢? 例如builtin包,那么执行go doc builtin;如果是http包,那么执行go doc net/http;查看某一个包里面的函数,那么执行godoc fmt Printf;也可以查看相应的代码,执行godoc -src fmt Printf;

通过命令在命令行执行 godoc -http=:端口号 比如godoc -http=:8080。然后在浏览器中打开127.0.0.1:8080,你将会看到一个golang.org的本地copy版本,通过它你可以查询pkg文档等其它内容。如果你设置了GOPATH,在pkg分类下,不但会列出标准包的文档,还会列出你本地GOPATH中所有项目的相关文档,这对于经常被限制访问的用户来说是一个不错的选择。

8、其他命令

go fix 用来修复以前老版本的代码到新版本,例如go1之前老版本的代码转化到go1

go version 查看go当前的版本

go env 查看当前go的环境变量

go list 列出当前全部安装的package

go run 编译并运行Go程序

对于不知道的命令,可以通过git help进行查看。

相关推荐:golang教程

The above is the detailed content of What are the commonly used commands in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn