API Commands

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

goctl api is one of the core modules in goctl. It can quickly generate an api service through the .api file with one click. If you just start a go-zero api demo project, You can complete an api service development and normal operation without even coding. In traditional api projects, we have to create directories at all levels, write structures, Define routing, add logic files, this series of operations, if calculated according to the business requirements of a protocol, it takes about 5 to 6 minutes for the entire coding to actually enter the writing of business logic. This does not consider the various errors that may occur during the writing process. With the increase of services and the increase of agreements, the time for this part of the preparation work will increase proportionally. The goctl api can completely replace you to do this part of the work, no matter how many agreements you have, in the end, it only takes less than 10 seconds to complete.

[!TIP] The structure is written, and the route definition is replaced by api, so in general, it saves you the time of creating folders, adding various files and resource dependencies.

API command description

  1. $ goctl api -h
  1. NAME:
  2. goctl api - generate api related files
  3. USAGE:
  4. goctl api command [command options] [arguments...]
  5. COMMANDS:
  6. new fast create api service
  7. format format api files
  8. validate validate api file
  9. doc generate doc files
  10. go generate go files for provided api in yaml file
  11. java generate java files for provided api in api file
  12. ts generate ts files for provided api in api file
  13. dart generate dart files for provided api in api file
  14. kt generate kotlin code for provided api file
  15. plugin custom file generator
  16. OPTIONS:
  17. -o value the output api file
  18. --help, -h show help

As you can see from the above, according to the different functions, the api contains a lot of self-commands and flags, let’s focus on it here The go subcommand, which function is to generate golang api services, let’s take a look at the usage help through goctl api go -h:

  1. $ goctl api go -h
  1. NAME:
  2. goctl api go - generate go files for provided api in yaml file
  3. USAGE:
  4. goctl api go [command options] [arguments...]
  5. OPTIONS:
  6. --dir value the target dir
  7. --api value the api file
  8. --style value the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]

Usage example

  1. $ goctl api go -api user.api -dir . -style gozero

Guess you wants