4

I have a function like the following for querying a mongo database:

func findEntry(db, table string, entry *User, finder *bson.M) (err error) {
    c := mongoSession.DB(db).C(table)
    return c.Find(finder).One(entry)
}

I'd like to reuse the function for structs other than "User", by passing in a pointer to any instantiated struct object - just not quite sure of the proper semantics to do this. I think that I should be able to do this by making the 'entry' parameter an interface{} and then I'd need to use reflection to 'cast' it back to the original struct so the One() function call could properly fill in the struct on the call? Is there a 'better' way to accomplish this (please no flaming about lack of generics, I'm just looking for a practical solution using best practices).

2
  • 1
    that's how you do it, no other way :) and no flaming about idioms, too. Commented Dec 15, 2014 at 13:54
  • If your function is really just one line long (spread over two lines): Why not have several of these functions like findUser findOther, findSomethingElse... ? Commented Dec 15, 2014 at 14:32

1 Answer 1

4

Use this function:

func findEntry(db, table string, entry interface{}, finder bson.M) error {
    c := mongoSession.DB(db).C(table)
    return c.Find(finder).One(entry)
}

and call it like:

var user User
err := findEntry("db", "users", &user, bson.M{"name": "John"})

The type information for user is passed through findEntry to the One method. There's no need for reflection or a "cast" in findEntry.

Also, use bson.M instead of *bson.M. There's no need to use a pointer here.

I created an example on the playground to show that the type information is passed through findEntry.

Sign up to request clarification or add additional context in comments.

4 Comments

entry needs to be asserted like so val, ok := entry.(*User). And then send that val into the One method
@Minty The type assertion is not required. Run this playground example to see a demonstration of the type information passing through a function similar to OP's findEntry function.
its true if you are using fmt package. But what if you are using third party API, like one used above? It needs a reference to concrete structure to "unmarshal" data.
Oh I see. I didnt know this API used reflection under the hood. Then yes, for this example you dont need assertion.

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.