How to Use go generate for Code Generation

Cli
Run go generate to execute code generation commands defined in //go:generate directives within your source files.

Use go generate to run code generation commands defined in //go:generate directives within your source files.

  1. Add a directive comment above the package statement specifying the command to run.
  2. Execute the go generate command in the directory containing the file.
//go:generate go run gen.go
package main

func main() {
	// Your code here
}
go generate