Home  >  Article  >  Backend Development  >  What is the function of go generate command?

What is the function of go generate command?

青灯夜游
青灯夜游Original
2023-01-30 15:07:171160browse

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.

What is the function of go generate command?

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.

Usage method

  • Add special comments
//go:generate command argument...
  • Execute the generate command
$ go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]

Note

  • 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).

Usage example

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.

What is the function of go generate command?

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.

What is the function of go generate command?

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!

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