我想建立一個基礎設施,它是項目的一個包。以便其他開發人員可以導入此包對數據庫執行 CRUD 操作。但是我在測試過程中遇到了一個錯誤:type Students struct { Name string Age int}type InsertOneResult struct { InsertedID interface{}}func dbGetOne(coll, document interface{}) (*InsertOneResult, error) {...}func dbUpdateOne(coll, document interface{}) (*InsertOneResult, error) { ...}func dbDeleteOne(coll, document interface{}) (*InsertOneResult, error) { ...}func dbInsertOne(coll, document interface{}) (*InsertOneResult, error) { res, err := coll.InsertOne(context.TODO(), document) if err != nil { log.Fatal(err) } return &InsertOneResult{InsertedID: res[0]}, err}func main() { client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://<user>:<password>@<host>:<port>/<dbname>")) if err != nil { log.Fatal(err) } ctx, _ := context.WithTimeout(context.Background(), 30*time.Second) err = client.Connect(ctx) if err != nil { log.Fatal(err) } coll := client.Database("db").Collection("students") data := Students{"Amy", 10} res, err := dbInsertOne(coll, data) if err != nil { log.Fatal(err) } fmt.Printf("inserted document with ID %v\n", res.InsertedID)}這是錯誤:./main.go:24:18: coll.InsertOne undefined (type interface {} is interface with no methods)有沒有辦法解決這個問題?提前致謝。
如何創建一個 mongo db 包
慕無忌1623718
2022-06-01 17:36:01