以下是 openapi 类型与 golang 字段类型的映射表:
OpenAPI 类型 | Golang 字段类型 |
---|---|
string | string |
number | float64 |
integer | int |
boolean | bool |
array | []T |
object | struct |
以下是一些样例:
- OpenAPI 定义:
type: object
properties:
name:
type: string
age:
type: integer
height:
type: number
is_student:
type: boolean
对应的 Golang 结构体定义:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Height float64 `json:"height"`
IsStudent bool `json:"is_student"`
}
- OpenAPI 定义:
type: object
properties:
users:
type: array
items:
type: object
properties:
name:
type: string
age:
type: integer
email:
type: string
对应的 Golang 结构体定义:
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Email string `json:"email"`
}
type UserList struct {
Users []User `json:"users"`
}