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

What is the function of go generate command?

Jan 30, 2023 pm 03:07 PM
golang go language

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
Copy after login

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...
Copy after login
  • Execute the generate command
$ go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
Copy after login

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!")}
Copy after login

Execute go generate command

$ go generate
GoGoGo!go rum main.go!amd64 darwin main.go 7 main
Copy after login

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
Copy after login

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)
Copy after login

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
Copy after login

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)}
Copy after login

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
Copy after login

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]]}
Copy after login

In this way, every time we modify the Pill type, all we need to do is run the following statement.

$ go generate
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

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. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

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

How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? Apr 02, 2025 pm 02:15 PM

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

How to solve the problem that custom structure labels in Goland do not take effect? How to solve the problem that custom structure labels in Goland do not take effect? Apr 02, 2025 pm 12:51 PM

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

How to ensure concurrency is safe and efficient when writing multi-process logs? How to ensure concurrency is safe and efficient when writing multi-process logs? Apr 02, 2025 pm 03:51 PM

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 provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

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 use Golang to implement Caddy-like background running, stop and reload functions? How to use Golang to implement Caddy-like background running, stop and reload functions? Apr 02, 2025 pm 02:12 PM

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

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

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

See all articles