2

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 Answer 1

6

The two otions both declare a variable of type *types.Course. The first pointer value is nil. The second is initialized to point at a value of type types.Course.

 var result *types.Course    // result == nil
 result := &types.Course{}   // result != nil, points to a value.
 result := new(types.Course) // basically the same as the second

The mgo function requires a pointer to a value. A nil pointer does not point to a value.

The typical way to write this code is:

var result types.Course   // declare variable of result type, not a pointer
err := s.collection.Find(bson.M{
    "department":    department,
    "course_number": number,
}).One(&result)           // pass address of value to function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.