うーん、私はインターフェイスに頭を悩ませています。
だから私はmongodbのものを処理するためにGoパッケージを使用していますが、そのパッケージをすべてのモデルにインポートしたくありません。サブパッケージ (モデルなど) の多くを標準ライブラリだけに保持したいと考えています。だから私はいくつかのインターフェースを次のようにレイアウトすると思った:
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
}
問題は、次のような関数を使用するときです。
func FindItem(defindex int, d database) (*Item, error) {
これは、mgo.Database を渡すことによって、インターフェイスを使用しているパッケージに含まれています。
item, err := dota.FindItem(int(defindex), ctx.Database)
コンパイラ エラーが発生します。
controllers/handlers.go:35: ctx.Database (タイプ *mgo.Database) を関数引数のタイプ dota.database として使用することはできません: *mgo.Database は dota.database を実装していません (C メソッドの間違ったタイプ) have C(string) ) *mgo.Collection が欲しい C(文字列) dota.collection
この概念について何が欠けていますか?