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.
go language automation tool
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.
- Add special comments
//go:generate command argument...
- Execute the generate command
$ go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
- This special comment is required Contained in the .go source code file.
- Each source code file can contain multiple generate special comments.
- go generate will not be triggered by commands such as go build, go get, go test, etc., and must be used explicitly by the developer.
- Command execution is serial. If an error occurs, subsequent commands will not be executed.
- Special comments must start with "//go:generate", with no space after the double slash.
- The execution command must be an executable program under the system PATH (echo $PATH).
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
Implementing String method for enumeration constants
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.
Summary
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...

Regarding the problem of custom structure tags in Goland When using Goland for Go language development, you often encounter some configuration problems. One of them is...

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to implement background running, stopping and reloading functions in Golang? During the programming process, we often need to implement background operation and stop...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
