I have a:
type User struct {
UID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Sex int `json:"sex"`
Nickname string `json:"nickname"`
ScreenName string `json:"screen_name"`
BDate string `json:"bdate"`
}
code:
var userList []*User
json.Unmarshal(resp.Response, &userList)
server response may be:
[{"id":1,"first_name":"name","last_name":"last","sex":2,"nickname":"","screen_name":"screen","bdate":"1.1.1"}]
or
[{"id":1,"first_name":"name","sex":2,"nickname":"","screen_name":"screen"}]
my problem when calling a nonexistent KEY error runtime "userList[0].LastName
":
error: index out of range
in the first case, the server returned:
"last_name":"last" and "bdate":"1.1.1"
in the second no
When there is no user, the server returns.
[{"error_code": 0, "error_msg": "Invalid user id"}]
answer:
if len(userList) > 0 {
c.String(http.StatusOK, "People name: %s", userList[0].FirstName)
} else {
c.String(http.StatusOK, "No People: %s", login)
}
Thank Magestro