0

Could anyone advise how to overcome this problem please ?

    func GetUserController(c echo.Context) error {
    id := c.Param("id")
    finduser := users[id]
    return c.JSON(http.StatusOK,finduser)}

    var users []User

    type User struct {
    Id       int    `json:"id"`
    }`

Results in error: invalid argument: index id (variable of type string) must be integer

jub0bs
  • 60,866
  • 25
  • 183
  • 186
wupssie
  • 39
  • 3

2 Answers2

1

it return type string, you should convert into int

id, _ := strconv.Atoi(c.Param("id"))

    
0

c.Params always returns string. You are trying to use a string value for array index which is not possible.

finduser := users[id]

id is a string. It should be int value.

id := c.Param("id")
id, _ := strconv.Atoi(id)
// Check for err
finduser := users[id]
Alp Tahta
  • 65
  • 7