API File Coding

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

Create file

  1. $ vim service/user/cmd/api/user.api
  1. type (
  2. LoginReq {
  3. Username string `json:"username"`
  4. Password string `json:"password"`
  5. }
  6. LoginReply {
  7. Id int64 `json:"id"`
  8. Name string `json:"name"`
  9. Gender string `json:"gender"`
  10. AccessToken string `json:"accessToken"`
  11. AccessExpire int64 `json:"accessExpire"`
  12. RefreshAfter int64 `json:"refreshAfter"`
  13. }
  14. )
  15. service user-api {
  16. @handler login
  17. post /user/login (LoginReq) returns (LoginReply)
  18. }

Generate api service

By goctl executable file

  1. $ cd book/service/user/cmd/api
  2. $ goctl api go -api user.api -dir .
  1. Done.

By Intellij Plugin

Right-click on the user.api file, and then click to enter New->Go Zero->Api Code, enter the target directory selection, that is, the target storage directory of the api source code, the default is the directory where user.api is located, select Click OK after finishing the list.

ApiGeneration ApiGenerationDirectorySelection

By Keymap

Open user.api, enter the editing area, use the shortcut key Command+N (for macOS) or alt+insert (for windows), select Api Code, and also enter the directory selection pop-up window, after selecting the directory Just click OK.

Guess you wants