Home >Backend Development >Golang >What is the function of go generate command?
The "go generate" command is used to automatically generate certain types of code before compilation; it is often used to automatically generate code, and it can generate code based on source code before the code is compiled. When the "go generate" command is run, it will scan the source code files related to the current package, find all special comments containing "//go:generate", extract and execute the command following the special comment.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language provides a series of powerful tools. Flexible use of these tools can make our project development easier. The toolset includes the following.
bug start a bug report build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get add dependencies to current module and install them install compile and install packages and dependencies list list packages or modules mod module maintenance run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages
The source code of the tool is located in $GOPATH/src/cmd/internal. This article mainly discusses the Go tool generate.
The go generate command is a newly added command in Go language version 1.4, which is often used for automatic Generate code, which generates code from source code before the code is compiled. When running go generate, it will scan the source code files related to the current package, find all comment statements containing "//go:generate", extract and execute the command after the comment, and the command will be an executable program. The process is similar to calling and executing a shell script.
//go:generate command argument...
$ go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
package mainimport "fmt"//go:generate echo GoGoGo!//go:generate go run main.go//go:generate echo $GOARCH $GOOS $GOFILE $GOLINE $GOPACKAGEfunc main() { fmt.Println("go rum main.go!")}
Execute go generate command
$ go generate GoGoGo!go rum main.go!amd64 darwin main.go 7 main
After reading the above brief introduction of generate, readers may not feel the power of this tool. Xiaocai Knife provides a The classic application scenario of this tool: implementing the String method for enumeration constants.
Another official tool, stringer, needs to be mentioned here, which can automatically write the String() method for a set of integer constants. Since stringer is not in the tool set of the official Go release, we need to install it ourselves and execute the following command.
go get golang.org/x/tools/cmd/stringer
Here is an example quoted from the stringer documentation. The code is as follows, which defines a set of integer constants of different Pill types.
package painkillertype Pill intconst ( Placebo Pill = iota Aspirin Ibuprofen Paracetamol Acetaminophen = Paracetamol)
For debugging or other reasons, we want these constants to be printed, which means Pill must have a signed method.
func (p Pill) String() string
To achieve it, it is very simple.
func (p Pill) String() string { switch p { case Placebo: return "Placebo" case Aspirin: return "Aspirin" case Ibuprofen: return "Ibuprofen" case Paracetamol: // == Acetaminophen return "Paracetamol" } return fmt.Sprintf("Pill(%d)", p)}
Just imagine, if a batch of new drug names are added to our Pill list, every time the drug name is added or modified, the corresponding signature function also needs to be changed. Wouldn't this be cumbersome and likely to be missed or wrong? At this time, we can solve this problem through go generate stringer. It's very simple, just add a comment statement to the code that defines Pill.
//go:generate stringer -type=Pill
The above command represents running the stringer tool to generate a String method for the Pill type. By default, it is output to the pill_string.go file. The execution is as follows.
$ go generate $ cat pill_string.go // Code generated by stringer -type Pill pill.go; DO NOT EDIT. package painkillerimport "fmt"const _Pill_name = "PlaceboAspirinIbuprofenParacetamol"var _Pill_index = [...]uint8{0, 7, 14, 23, 34}func (i Pill) String() string { if i = Pill(len(_Pill_index)) { return fmt.Sprintf("Pill(%d)", i) } return _Pill_name[_Pill_index[i]:_Pill_index[i+1]]}
In this way, every time we modify the Pill type, all we need to do is run the following statement.
$ go generate
Of course, if you find this troublesome, or are worried about forgetting to execute the generate statement. Then, you can write the go generate statement into the Makefile and place it before the go build command to automate code generation and compilation.
It is worth mentioning that in the Go source code documents, the go generate stringer solution is widely used to implement the String method for enumeration constants. Under the source code of Xiaocai Knife's native Go 1.14.1, there are a total of 23 uses, as follows.
This article mainly introduces what generate is, what it can do, if you want To deeply understand its internal implementation logic, you can look at the detailed process of generating code in the Go source code, such as the generation of zfuncversion.go through genzfunc.go under the sort package. In the Go source code treasure house, you can find many similar implementation logics, please refer to the following.
They use the libraries provided by the Go compiler, including go/ast for defining abstract syntax trees, go/parser for parsing abstract syntax trees, go/format for parsing code formatting, and go/ for Go lexical tags. token, etc. Parse the source file and generate new code according to the existing template. This process is similar to using templates to generate HTML files in Web services.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is the function of go generate command?. For more information, please follow other related articles on the PHP Chinese website!