我无法使用从请求中获取的数据创建结构

问题描述 投票:-2回答:1

我正在尝试获取json在我的API中传递的值,例如下面的示例:

Insomnia data

代码:

handle code

结构

Gerente struct

我已经尝试使用`json:"nome"`

我已经尝试过更改声明,实例化结构的方法,并且已经尝试了几种方法来获取值和创建“ gerente对象”

结果始终相同。

enter image description hereenter image description here

json go struct mux insomnia
1个回答
0
投票

要解压缩数据的结构中的字段需要导出。

在Golang中,以小写字母开头的字段或变量被声明为unexported。无法从其他包(包括encoding/json)访问这些包,您正在使用这些包对请求正文进行解码。如果需要从当前程序包外部访问结构域或变量,则它们应以大写字母开头,声明为exported

也有时不需要它们,但是您应该添加标记,该标记指示应将哪些数据解析到哪个字段中。

type Gerente struct {
    Email string `json:"email"`
    Nome  string `json:"nome"`
    Senha string `json:"senha"`
}
© www.soinside.com 2019 - 2024. All rights reserved.