dongtan9465
2015-11-19 04:12
浏览 55

Golang MongoDB(mgo)查找反射错误

With the following code

func (s Store) Lookup(department string, number string) (*types.Course, error) {
    var result *types.Course
    err := s.collection.Find(bson.M{
        "department":    department,
        "course_number": number,
    }).One(result)

    if err != nil {
        switch err {
        case mgo.ErrNotFound:
            return nil, ErrNotFound
        default:
            log.Error(err)
            return nil, ErrInternal
        }
    }

    return result, nil
}

I came across the error:

reflect: reflect.Value.Set using unaddressable value

If I change the first line from var result *types.Course to result := &types.Course{} there is no error. What exactly is the difference between these two?

1条回答 默认 最新

相关推荐 更多相似问题