dongnachuang6635 2018-03-02 11:51
浏览 664

接口转换:接口{}是int64,不是[] 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!

  • 写回答

1条回答 默认 最新

  • dongluo1853 2018-03-02 13:30
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题