Code Generating - 图1info

Starting from version v2, the latest version of the CLI tool features will be compiled with the latest version of the GoFrame framework. If there is a compatibility issue between the auto-generated code of the local CLI tool and the project’s GoFrame framework version, it is recommended to upgrade the project framework version or customize the installation of an older version of the CLI tool. For the installation method of the old version of the CLI tool, refer to the repository homepage introduction: https://github.com/gogf/gf-cli

Important Note🔥

  • The code generation function provided by the CLI tool aims to standardize project code writing, simplify project development complexity, and allow developers to focus on business logic itself.
  • The CLI tool itself requires a certain degree of prior learning and understanding cost (try to understand why), but once proficient, everyone’s development work will be twice as effective with half the effort.
  • The code generation function of the CLI tool will be highly beneficial for enterprise-level projects and multi-member team projects. However, for small single-person projects, developers can evaluate whether to use it based on personal preference. The GoFrame framework itself only provides basic components and adopts a modular and flexible design without strict requirements on project code; however, the CLI tool will have some restrictions to ensure that each member of the team maintains consistent pace and style, preventing developers from writing code too freely.

Usage

  1. $ gf gen -h
  2. USAGE
  3. gf gen COMMAND [OPTION]
  4. COMMAND
  5. ctrl parse api definitions to generate controller/sdk go files
  6. dao automatically generate go files for dao/do/entity
  7. enums parse go files in current project and generate enums go file
  8. pb parse proto files and generate protobuf go files
  9. pbentity generate entity message files in protobuf3 format
  10. service parse struct and associated functions from packages to generate service go file
  11. DESCRIPTION
  12. The "gen" command is designed for multiple generating purposes.
  13. It's currently supporting generating go files for ORM models, protobuf and protobuf entity files.
  14. Please use "gf gen dao -h" for specified type help.

Documentation

📄️ Controller GeneratingGenerate API interface controllers and SDK code using GoFrame to help developers reduce repetitive code work, standardize API and controller code structure, and improve collaborative development efficiency. Additionally, it offers the functionality to generate HTTP SDK code for easy internal and external service calls. Code generation can be performed using command-line mode and automatic generation mode, and detailed command parameter instructions and usage examples are provided.

📄️ Dao/Do/Entity GeneratingUsage and parameter configuration of the gen dao command in the GoFrame framework. The gen dao command is a key tool for generating data access objects, data transformation models, and entity data models, supporting detailed configuration through command line parameters and configuration files, applicable to various database types. By flexibly using command options, different project code generation requirements can be met, ensuring the implementation of engineering design specifications.

📄️ Service GeneratingEncapsulate and manage business logic in the GoFrame framework by generating module API definitions and registration code to simplify the implementation process of separating business logic from interfaces. Improve module transparency through structured coding of logic and interfaces while avoiding circular dependencies. Provides both manual and automated implementation modes suitable for different development environments.

📄️ Enums MaintenanceUse GoFrame command line tool to maintain and generate enum value information, especially for enum parameters in OpenAPIv3 documents. Automatically generate and load enum values by parsing source code to reduce manual maintenance costs, improve development efficiency, and enhance collaboration between backend and frontend.

📄️ Protobuf CompilationUse the command line tool in the GoFrame framework to compile proto files, generating the corresponding protobuf Go files and controller files. With the gf gen pb command, users can set different paths to store the generated interface and controller files, meeting the needs of project engineering. Additionally, this article lists the usage guide and precautions for this command, allowing developers to better use this feature.

📄️ DB Table To ProtobufUse the GoFrame framework’s command line tool gf to generate proto data structure files pbentity based on database tables. Includes details on command usage, option configuration, and explanations, as well as the differences from the entity files generated in the gen dao module. Applicable for generating data entity structures for HTTP and GRPC services, supporting configuration of generation rules for multiple databases.