Hey guys,
I am fairly new to GoLang, still learning.
I am trying to working on a small utility, which fetches details from a third party.
As per their documentation, I will get response something like
{
"users": [
[
{
"firstName": "string ",
"lastName": "string",
"username": "string",
"email": "string",
"createdAt": "string",
"passwordLastUpdated": "string",
"verified": true,
"_selfUrl": "string"
},
{
"firstName": "string ",
"lastName": "string",
"username": "string",
"email": "string",
"createdAt": "string",
"passwordLastUpdated": "string",
"verified": true,
"_selfUrl": "string"
}
]
]
}
For this, I created struct type
type V1User struct {
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
UserName string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
PasswordLastUpdated string `json:"passwordLastUpdated,omitempty"`
Verified bool `json:"verified,omitempty"`
SelfUrl string `json:"_selfUrl,omitempty"`
}
type allUsers struct {
users []V1User `json:"users,omitempty"`
}
I am pretty sure I am missing out on something. I am running below code to fetch and decode the data
func (c *Client) getUsers() (users []allUsers, err error) {
resp, err := c.request(http.MethodGet, "user", nil)
if err != nil {
return
}
defer resp.Body.Close()
var result = struct {
Users []allUsers
}{}
err = json.NewDecoder(resp.Body).Decode(&result)
return result.Users, err
}
and I get error
fmt.Println(result.Users) -> Users: [{[]}]
fmt.Println(err) -> Error: json: cannot unmarshal array into Go struct field .Users of type main.allUsers
I am still trying to figure out what's wrong here, any help will be appreciated.