Ready to build your first API with GoFrame? This chapter will show you how to create professional-grade CRUD endpoints using GoFrame’s scaffolding tools. Whether you’re new to Go or an experienced developer, you’ll find our streamlined approach both powerful and intuitive.
tip
Find the complete source code for this tutorial at: https://github.com/gogf/quick-demo
📄️ Step1 - Design Data StructureDesign and apply MySQL data tables. We define the structure of data tables for user information and practice operations by running MySQL with Docker, including creating table structures and applying SQL statements, helping you quickly master database operation skills.
📄️ Step2 - Generate dao/do/entityUse the scaffolding tool in the GoFrame framework for automated data access object generation, ensure the CLI tool is configured correctly, then execute the code generation through commands, and generate corresponding dao, do, and entity files after creating the database tables to simplify CRUD operations. It demonstrates the specific file structure and usage, as well as the principles of generating different types of files.
📄️ Step3 - Write API DefinitionDefine CRUD APIs in the project’s api directory, adopting a RESTful style of API design, using HTTP Method to standardize API requests. API definitions use g.Meta to manage metadata information, including route address, request method, and API description. The request parameters and return data structures define detailed parameter validation rules. Starting with version v1 for API version control to maintain future compatibility. APIs flexibly receive parameters to meet the diverse needs of API requests.
📄️ Step4 - Generate ControllerGenerate controller code based on API definition, including API API files, route object management, and route implementation code, using GoFrame framework’s command tool to quickly generate relevant code templates, ensuring complete implementation of the API, and demonstrating how to implement specific business logic through files.
📄️ Step5 - Implement API LogicUse GoFrame framework to complete API logic implementation. By using the project scaffolding, pre-generate code unrelated to project business logic and focus on business logic implementation. The specific implementation process of CURD operations, including creation, update, deletion, and retrieval of APIs, is introduced. It elaborates on parameter validation, form data insertion and update, intelligent data mapping, validation mechanism, and efficiently utilizing GoFrame framework functionalities during data operations.
📄️ Step6 - ConfigurationIntroduce MySQL database driver, with steps for adding database configuration and route registration. Detailed explanation of configurations in the scaffold project template, including tool configuration and business configuration, and how to modify configuration files.
📄️ Step7 - Run and TestStart the service through the command line and generate API documentation using Swagger. Supports RESTful API APIs for creating users, querying user information, modifying user data, and deleting users. Also supports API testing using the curl command, providing detailed validation rules and error codes to ensure data accuracy and reliability.