dongshengheng1013 2016-06-25 23:28
浏览 331

Golang MongoDB错误:结果参数必须是切片地址

I have a function that needs to retrieve all users from a mongoDB collection which is throwing an error when I try to call it through a REST endpoint. The function is:

func (usercontroller UserController) GetAllUsers(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        session := usercontroller.session.Copy()
        defer session.Close()

        // Stub user
        result := models.User{}

        // get all users
        if err := session.DB("Auth").C("users").Find(nil).All(&result); err != nil {

        // Marshal provided interface into JSON structure
        mResult, _ := json.Marshal(result)

        // Write content-type, statuscode, and the data package
        w.Header().Set("Content-Type", "application/json")
        fmt.Fprintf(w, "%s", mResult)

The error is:

2016/06/25 16:00:21 http: panic serving result argument must be a slice address
goroutine 13 [running]:
    /usr/lib/go-1.6/src/net/http/server.go:1389 +0xc1
panic(0x729b60, 0xc82000ba50)
    /usr/lib/go-1.6/src/runtime/panic.go:443 +0x4e9*Iter).All(0xc8201280f0, 0x704600, 0xc82007e360, 0x0, 0x0)
    /root/work/src/ +0x742*Query).All(0xc8200bc600, 0x704600, 0xc82007e360, 0x0, 0x0)
    /root/work/src/ +0x4f
Auth-REST-Server/controllers.UserController.GetAllUsers(0xc82000d040, 0x7f3c1b9bf950, 0xc820061c70, 0xc8200c6380, 0x0, 0x0, 0x0)
    /root/work/src/Auth-REST-Server/controllers/user.go:34 +0x251
Auth-REST-Server/controllers.(UserController).GetAllUsers-fm(0x7f3c1b9bf950, 0xc820061c70, 0xc8200c6380, 0x0, 0x0, 0x0)
    /root/work/src/Auth-REST-Server/server.go:21 +0x5f*Router).ServeHTTP(0xc820016380, 0x7f3c1b9bf950, 0xc820061c70, 0xc8200c6380)
    /root/work/src/ +0x195
net/http.serverHandler.ServeHTTP(0xc820122000, 0x7f3c1b9bf950, 0xc820061c70, 0xc8200c6380)
    /usr/lib/go-1.6/src/net/http/server.go:2081 +0x19e
    /usr/lib/go-1.6/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
    /usr/lib/go-1.6/src/net/http/server.go:2137 +0x44e

What am I doing wrong here. I thought I could just call Find(nil).All(&result) and retrieve all of the records accordingly.

  • 写回答

1条回答 默认 最新

  • duan5991518 2016-06-26 00:58

    In your code you have:

    // Stub user
    result := models.User{}

    So result represents a single user, but you want all the users from the database, so the following should do the job:

    // Stub user
    result := make([]models.User, 0, 10) // Here you can specify a len and a cap.

    This way, result is a slice of models.User.

    Also, in Golang, you should never ignore an error:

    // Marshal provided interface into JSON structure
    mResult, _ := json.Marshal(result)

    Should be:

    // Marshal provided interface into JSON structure
    mResult, err := json.Marshal(result)
    if err != nil {
        // do something with the error (log it or write it in the response)

    EDIT: I updated the code, sorry for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?