0

Following code has the err:

Error:

controllers/controllers.go:80:53: cannot use c.Param(id) (type string) as type int in argument to services.GetCharactersID controllers/controllers.go:80:54: undefined: id)

func GetCharactersID (c *gin.Context) {
    characters, err := services.GetCharactersID(c.Param("id"))
    if err!=nil {
        response:= domain.Response{Mensaje:err.Error()}
        c.JSON(http.StatusInternalServerError,response)
        return
    }
    c.JSON (http.StatusOK,characters)
}
//Services

func GetCharactersID (id int) (domain.Characters,error) {
    var err error
    characterss,err := GetCharactersID(id)
    s := strconv.Itoa(id)
    ts := "my_hashjavi"
    hash := GetHashString(ts+private_key+public_key)
    var url string = "https://gateway.marvel.com:443/v1/public"
    url += "/characters" + "/"
    url+= s + "?"
    url += "ts="+ts
    url += "&apikey="+ public_key
    url += "&hash="+ hash
    resp,err1 := http.Get(url)
    Data, err2 := ioutil.ReadAll(resp.Body)

    if err1 != nil || err2 != nil {
        err= errors.New("Error al obtener los personajes")
        return characterss,err
    }
    json.Unmarshal(Data,&characterss)
    return characterss , nil

}
Tiya Jose
  • 1,359
  • 14
  • 24
Javier
  • 11
  • 1
  • 4
  • URL parameters are strings so ```func (c *Context) Param(key string) string``` returns a string; but your function ```GetCharactersID``` accepts an ```int```. If the paramater is numeric then you can parse the string using [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi). – Brits Feb 13 '20 at 19:14
  • How can I do?the errors is here (controllers/controllers.go:80:53: cannot use c.Param(id) (type string) as type int in argument to services.GetCharactersID controllers/controllers.go:80:54: undefined: id) func GetCharactersID (c *gin.Context) { characters, err := services.GetCharactersID(c.Param(id)) if err!=nil { response:= domain.Response{Mensaje:err.Error()} c.JSON(http.StatusInternalServerError,response) return } c.JSON (http.StatusOK,characters) } – Javier Feb 13 '20 at 20:40
  • Please edit your question rather than posting lots of code in the comments. If I understand you correctly then you need to edit ```characters, err := services.GetCharactersID(c.Param("id"))``` to do three steps - firstly get the param (say ```p := c.Param("id")```) then convert it (```pi, err := strconv.Atoi(p)``` - do something with the error!) and finally make the call (```services.GetCharactersID(pi)```). Note: the first two steps can be combined into one line. – Brits Feb 13 '20 at 20:51

1 Answers1

0

I dont have your full code, but I refactor it and apply your fix about id is not a int

func GetCharactersID(c *gin.Context) {
    characters, err := services.GetCharactersID(c.Param("id"))
    if err != nil {
        response := domain.Response{Mensaje: err.Error()}
        c.JSON(http.StatusInternalServerError, response)
        return
    }
    c.JSON(http.StatusOK, characters)
}

func GetCharactersID(sID string) (domain.Characters, error) {
    base := "https://gateway.marvel.com:443/v1/public"
    ts := "my_hashjavi"
    id, err := strconv.Atoi(sID)
    if err != nil {
        return characterss, errors.New("xxx...")
    }

    characterss, err := GetCharactersID(sID)
    if err != nil {
        return characterss, errors.New("xxx...")
    }

    hash := GetHashString(ts + private_key + public_key)
    url := fmt.Sprint("%s/characters/%d?ts=%s&apikey=%s&hash=%s",
        base, sID, ts, public_key, hash)

    resp, err := http.Get(url)
    if err != nil
        return characterss, errors.New("xxx...")
    }

    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return characterss, errors.New("xxx...")
    }

    err = json.Unmarshal(data, &characterss)
    if err != nil {
        return characterss, err
    }

    return characterss, errors.New("xxx...")
}
J-Jacques M
  • 978
  • 6
  • 16