接口转换:interface {}是int64,而不是[] uint8

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口转换:interface {}是int64,而不是[] uint8相关的知识,希望对你有一定的参考价值。

我正在尝试实现一个go程序,它可以处理http请求并以嵌套的JSON发送响应。当我运行我的代码并调用URL时,我收到运行时错误,这是什么意思?我怎么处理这个?

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

这是我的功能代码,在调用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)
}

提前致谢!

答案

我改变了这一行

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

values := make([]string, usercount)

而这一行

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

Result[usercolumns[i]] = v

以上是关于接口转换:interface {}是int64,而不是[] uint8的主要内容,如果未能解决你的问题,请参考以下文章

将 [] 字符串转换为 [] 接口

public interface Command //接口里定Proocess方法用于封装“处理行为” void Process(int [] target);

go(golang)中的类型转换

java8函数式接口(Functional Interface)

解决:interface conversion: interface {} is float64, not int

[]int 能转换为 []interface 吗?