将结构字段名称声明为"类型"

Bha*_*ana 4 struct go

我正在集成API并在结构中解析其响应.为此,我需要声明字段名称,type因为API的响应包含keynamed type.但是,当我type在字段名称中声明时,它给出了错误:

语法错误:意外的文字type,期望字段名称或嵌入类型.

我不知道如何将literal声明type为struct field name.

我的结构是

type Test struct {
    active bool
    name string
    description string
    amount  int
    currency string
    type string
}
Run Code Online (Sandbox Code Playgroud)

小智 9

更新您的结构,以解组您需要导出结构的字段的 api响应,即您的结构字段应该是大写:

type Test struct {
    Active bool `json:"active"`
    Name string  `json:"name"`
    Description string `json:"description"`
    Amount  int `json:"amount"`
    Currency string `json:"currency"`
    Type string `json:"type"`
}
Run Code Online (Sandbox Code Playgroud)

然后尝试解组您对此结构的API响应