简体   繁体   中英

interface conversion: interface {} is int64, not []uint8

I'm trying to implement a go program which can handle http requests and send response in nested JSON. When i run my code and call the URL, I'm getting a runtime error,what does it mean? how can i handle this?

panic serving 192.168.0.101:50760: interface conversion: interface {} is int64, not []uint8
goroutine 5 [running]

This is my function code which is called on hitting the url

func logInPass(res http.ResponseWriter, req *http.Request) {
    type Resp struct {
        Result []map[string]interface{} `json:"Result,omitempty"`
        Status string                   `json:"Status"`
    }
    type AxleUser struct {
        Mobile   string `json:"Mobile"`
        Password string `json:"Password"`
    }

    var Response Resp
    Response.Status = "failed"
    Result := make(map[string]interface{})
    db, err := sql.Open("mysql", "root:chikkIbuddI57@tcp(127.0.0.1:3306)/b2b")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    rnd := render.New()
    b, err := ioutil.ReadAll(req.Body)
    defer req.Body.Close()

    if err != nil {
        panic(err.Error())
    }
    // Unmarshal the request body
    var msg AxleUser
    err = json.Unmarshal(b, &msg)
    if err != nil {
        panic(err.Error())
    }
    // get shop id from emp table using mobile number and password
    userrows, usererr := db.Query("SELECT b2b_emp_id,b2b_shop_id,b2b_shop_name,b2b_emp_name,b2b_emp_mobile_number FROM b2b_employee_tbl WHERE b2b_emp_mobile_number=? and b2b_password=?", msg.Mobile, msg.Password)
    if usererr != nil {
        panic(usererr.Error())
    }
    usercolumns, usererr := userrows.Columns()
    if usererr != nil {
        panic(usererr.Error())
    }

    usercount := len(usercolumns)
    values := make([]interface{}, usercount)
    scanArgs := make([]interface{}, usercount)
    for i := range values {
        scanArgs[i] = &values[i]
    }
    for userrows.Next() {
        usererr := userrows.Scan(scanArgs...)
        if usererr != nil {
            panic(usererr.Error())
        }
        for i, v := range values {
            if v != nil {
                Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))
            }
        }
        Response.Result = append(Response.Result, Result)
        Response.Status = "success"
    }

    res.Header().Set("Content-Type", "application/json")
    rnd.JSON(res, http.StatusOK, Response)
}

Thanks in Advance!

I've changed this line

values := make([]interface{}, usercount)

To

values := make([]string, usercount)

And this line

Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))

To

Result[usercolumns[i]] = v

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM