Question

Hmmm, I'm having a problem wrapping my head around interfaces.

So I am using a Go package for handling my mongodb stuffs, but I don't want to import that package into every model and what not. I'd like to keep as many of my sub packages (like models) to just the standard library. So I thought I would lay out some interfaces like so:

type m map[string]interface{}

type collectionSlice interface {
    One(interface{}) error
}

type collection interface {
    Upsert(interface{}, interface{}) (interface{}, error)
    Find(interface{}) collectionSlice
}

type database interface {
    C(string) collection
}

The problem is, when I go to use a function like:

func FindItem(defindex int, d database) (*Item, error) {

that is found in the package that is using the interfaces by passing in my mgo.Database:

item, err := dota.FindItem(int(defindex), ctx.Database)

I get a compiler error:

controllers/handlers.go:35: cannot use ctx.Database (type *mgo.Database) as type dota.database in function argument: *mgo.Database does not implement dota.database (wrong type for C method) have C(string) *mgo.Collection want C(string) dota.collection

What am I missing about this concept?

Was it helpful?

Solution

Got a reply to this answer on golang-nuts.

The issue I'm having is that the methods must have exactly the same signature: http://golang.org/doc/faq#t_and_equal_interface

Thanks to Jesse McNelis on the golang-nuts group!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top