This is a creation in
Article, where the information may have evolved or changed.
本文是学习Golang语言的系列文章之一,主要记录Go中浅显易懂的命令,希望让它们在脑海中的印象犹如初恋般印象深刻,永不忘记。
Go Build
This command is primarily used for test compilation. During the compilation of a package, the associated package can also be compiled at the same time, if necessary.
-普通包:执行完go build ,不会产生任何文件,如果需要在$GOPATH/pkg下生成相应文件,则要执行go install.-main 包:执行完go build,会在当前目录下生成一个可执行文件,如果需要在$GOPATH/bin下生成对应文件,需要执行go install 或使用 go build -o outputpath/如果只想编译某个文件,只需在在后面加上文件名即可,例如:go build hello.go非main包在默认情况下编译输出的是package名,main包是第一个源文件的文件包,也还可以指定编译输出的文件名,例如,go build -o xialingsc.exego build 会忽略目录下以"_"或"."开头的go文件如果源代码针对不同的操作系统需要不同的处理,那么可以根据不同的操作系统后缀来命名文件。例如,readfile_linux.go,readfile_drawin.go,readfile_windows.go.go build 会选择性编译文件,Linux系统只编译readfile_linux.go,其他文件则被忽略。
Go clean
Used to remove the generated files from the current source package, including, _obj (old objects directory, Makefiles Legacy),
_test (old test directory), _testmain.go (old gotest file), Test.out, build.out (old Test record)
*. [568ao] Object file, DIR (. exe) (generated by Go build), dir.test (. ext) (generated by Go test-c),
Mainfile (. exe) (generated by Go build mainfile.go)
The most important role of the command, after clearing the compiled files, upload git, keep the source clean.
Go FMT
Help to format the written code files, so that the code is not concerned about the format, after writing, easy to execute the go FMT file name. Go.
Improve efficiency. More often, you can use GOFMT and add-w parameters, otherwise the formatting results will not be written to the file, for example:
gofmt-w src to format the project.
Go get
Get remote code dynamically. This code is divided into two parts, one is to download the source package, the other step is to execute go install
In order for go get to work properly, you need to ensure that you have the appropriate source management tools installed and add these commands to path.
Learn more with Go help remote.
Go test
Executing this command will automatically read the source directory named *_test.go file, generate and run the test executable file.
By default, no parameters are required or parameters can be taken, see go help Testflag
Go doc
How do I view the corresponding package document?
If it is a builtin package, you can execute Go doc builtin; if it is an HTTP packet, execute go doc net/http;
View the functions within a package, similar to executing Godoc FMT Println, and view the corresponding code
GODOC-SRC FMT Println
The great thing is that you can execute the godoc-http=: port number in the terminal, for example, godoc-http=:8080, you can
Browser-typed 127.0.0.1:8080 to view the contents of the document.
Other commands
go fix 用来修复以前老版本的代码到新版本go version 查看go当前的版本go env 查看当前go的环境变量go list 列出当前全部安装的packagego run 编译并运行go语言程序