api file format
Overview
After the api file is written, the content of our api is uneven, as follows
syntax= "v1"
info(
key1:value1
key2:value2
longkey:longvalue
)
type Foo {
Bar string
Baz int
}
This api file is not read. We can use goctl api format
to format api files.
Task Targets
- familiar with the use of goctl api format
- Learn about the function of the goctl api format command
Preparing
Format api file
Create an api file and copy the following content to the
demo.api
filesyntax = "v1"
type User {
Id int64 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Description string `json:"description"`
}
type Student {
Id int64 `json:"id"`
No int64 `json:"no"`
Name string `json:"name"`
Age int `json:"age"`
Description string `json:"description"`
}
service User {
@handler ping
get /ping
}Create workspace and directory
$ mkdir -p ~/workspace/api/format
Copy the above
demo.api
files to~/workspace/api/format under directory
Format api file
$ cd ~/workspace/api/format
$ goctl api format --dir demo.apiView formatted api files
syntax = "v1"
type User {
Id int64 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Description string `json:"description"`
}
type Student {
Id int64 `json:"id"`
No int64 `json:"no"`
Name string `json:"name"`
Age int `json:"age"`
Description string `json:"description"`
}
service User {
@handler ping
get /ping
}