In the go language, the "go fmt" command is mainly used to help developers format the code files they have written. The "go fmt" command will format the code of all Go language source code files in the specified code package according to the Go language code specifications. All Go language source code files include command source code files, library source code files and test source code files. The "go fmt" command will only format Go language source code files that are directly saved in the directory corresponding to the specified code package.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
go fmt command introduction
For a programming language, code formatting is the most controversial issue. Different Developers may have different coding styles and habits, but if all developers can write code in the same format, developers can focus on the problem the language is trying to solve, saving development time.
The Go language development team has formulated a unified official code style and launched the gofmt tool (gofmt or go fmt) to help developers format their code into a unified style.
gofmt is a cli program that will read standard input first. If a file path is passed in, the file will be formatted. If a directory is passed in, all .go files in the directory will be formatted. If If no parameters are passed, all .go files in the current directory will be formatted.
There is also a go fmt
command in the Go language. The go fmt
command is a simple encapsulation of gofmt.
The go fmt command is mainly used to help you format the code files you have written [Many third-party integration software uses go fmt Command】
Usage:
go fmt <文件名>.go
Use go fmt command, more often gofmt is used, 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.
Parameter introduction
-l Display the files that need to be formatted
-w Put The rewritten content is written directly to the file instead of being printed to standard output as the result.
-r Add a rewrite rule in the form of "a[b:len(a)] -> a[b:]" to facilitate batch replacement
-s Simplify the code in the file
-d Display the diff before and after formatting instead of writing to the file, the default is false
-e Print all syntax errors to standard output. If you don't use this tag, only the first 10 errors on different lines will be printed.
-cpuprofile supports debugging mode and writes the corresponding cpufile to the specified file scope
go fmt and gofmt
The go fmt command will format the codes of all Go language source code files in the specified code package according to the Go language code specifications. All Go language source code files include command source code files and library source codes. files and test source code files. Note that when the code package also has sub-code packages, the Go language source code files in the sub-code packages are not included. In other words, the go fmt command will only format Go language source code files that are directly saved in the directory corresponding to the specified code package.
Similar to the relationship between go doc command and godoc command, go fmt command is a simple encapsulation of gofmt command. The go fmt command itself can accept two tags. The -n flag allows the command program to just print out the internally used gofmt command and its flags and arguments without actually executing it. The -x flag will cause the command program to both print and execute the command. Inside the go fmt command program, the markers -l and -w will be added after the gofmt command it calls, and the paths of all Go language source files in the specified code package will be used as parameters, like this:
hc@ubt:~$ go fmt -n pkgtool gofmt -l -w golang/goc2p/src/pkgtool/envir.go golang/goc2p/src pkgtoolenvir_test.go golang/goc2p/src/pkgtool/fpath.go golang/goc2p/src/pkgtool ipath.go golang/goc2p/src/pkgtool/pnode.go golang/goc2p/src/pkgtool/util.go golang/goc2p/src/pkgtool/util_test.go
Note that the path to the Go language source file as a parameter of the gofmt command is relative, not absolute. But this is just to make the parameters look shorter. Therefore, when we directly execute the gofmt command, there is no problem in using the absolute path of the source code file as a parameter. In fact, the relative path or absolute path of any Go source code file or directory containing Go language source code files can be used as parameters of the gofmt command. When using the absolute path or relative path of a directory containing Go language source code files as a parameter, the gofmt command will use the Go language source code files in this directory as the target source code file.
The tags added inside the go fmt command program when executing the gofmt command are fixed. If we want to use a different set of tags, we must use the gofmt command directly. Now let's take a look at all the tags accepted by the gofmt command. As shown in the table below.
Table 0-13 Mark description of gofmt command
Tag name | Tag description | |
---|---|---|
Put CPU The summary is written to the specified file. The path to the file should be used as the value of this tag. | ||
Shows the differences (if any) before and after formatting, rather than formatting those codes directly. | ||
Report all errors in the target source code file. By default, only the first 10 errors are displayed. | ||
Only print the absolute paths of source code files that do not meet the formatting specifications and need to be rewritten by the command program to the standard output. Instead of printing all the rewritten content to standard output. | ||
Add a rewrite rule in the form "a[b:len(a)] -> a[b:]". We need to use it if we need to customize some additional formatting rules. The rule string should be used as the value of this tag. | ||
Simplify the code in the file. | ||
Write the rewritten content directly to the file instead of printing the result to the standard output. |
功能 | go fmt 命令 | gofmt 命令 |
---|---|---|
格式化代码 | √ | √ |
列出不规范的源码文件 | √ | √ |
自动改写源码文件 | √ | √ |
显示对比信息 | × | √ |
提示全部错误 | × | √ |
简化代码 | × | √ |
自定义替换/重构辅助 | × | √ |
CPU概要记录 | × | √ |
最后,值得一提的是,当我们执行gofmt命令且没有加任何参数的时候,该命令将会进入到交互模式。在这种模式下,我们可以直接在命令行界面中输入源码,并以Ctrl-d结束。在Linux操作系统下,Ctrl-d代表EOF(End Of File,中文译为文件结束符)。需要注意的是,如果在一行的中间按下Ctrl-d,则表示输出“标准输入”的缓存区,所以这时必须连续按两次Ctrl-d。另外,在Windows操作系统下,Ctrl-z代表EOF,所以需要以Ctrl-z结束。在这之后,gofmt命令会像从源码文件中读取源码那样从命令行界面(也称为标准输入)读取源码,并在格式化后将结果打印到命令行界面(也称为标准输出)中。示例如下:
hc@ubt:~$ gofmt -r='fmt.Println(a)->fmt.Printf("%s\n", a)' if a=="print" {fmt.Println(a)} <----- 在此行的末尾键入回车和Ctrl-d。 warning: rewrite ignored for incomplete programs <----- 此行及以下就是命令输出的内容。 if a == "print" { fmt.Println(a) }
由上述示例可知,我们可以使用gofmt命令的交互模式格式化任意的代码片段。虽然会显示一行警告信息,但是格式化后的结果仍然会被打印出来。并且,在交互模式下,当我们输入的代码片段不符合Go语言的语法规则时,命令程序也会打印出错误提示信息。在其它方面,命令程序在交互模式与普通模式下的行为也是基本一致的。
The above is the detailed content of What is the function of go fmt command. For more information, please follow other related articles on the PHP Chinese website!