我創建這個結構:type Country struct {Id interface{} `bson:"_id,omitempty"`Languages []string `json:"languages"`Country string `json:"country"`Country_id int `json:"country_id"`Capital string `json:"capital"`Currency_name string `json:"currency_name"`Currency_symbol string `json:"currency_symbol"`Currency_code string `json:"currency_code"`Iso string `json:"iso"`}然后從 Mongo 獲得我需要的所有數據。問題是當我嘗試附加每個元素時,語言被覆蓋。這是附錄:func GetAllCountries() []models.Country { options := options.Find() options.SetLimit(4)cur, err := db.Collection(COLLCOUNTRIES).Find(context.Background(), bson.D{}, options)if err != nil { log.Fatal(err)}var elements []models.Countryvar elem models.Country// Get the next result from the cursorfor cur.Next(context.Background()) { err := cur.Decode(&elem) if err != nil { log.Fatal(err) } elements = append(elements, elem)}if err := cur.Err(); err != nil { log.Fatal(err)}cur.Close(context.Background())return elements}結果是這樣的:-- 追加之前的元素 -- {ObjectID("5d7f6b2b57d5104f58e53d2b") [ar-AE fa en hi ur] United Arab Emirates 290557 Abu Dhabi Dirham ?.? AED AE}-- 追加之前的元素 --{ObjectID("5d7f6b2b57d5104f58e53d2c") [fa-AF ps uz-AF tk] Afghanistan 1149361 Kabul Afghani ? AFN AF}——所有結果——[{ObjectID("5d7f6b2b57d5104f58e53d2b") [fa-AF ps uz-AF tk ur] United Arab Emirates 290557 Abu Dhabi Dirham ?.? AED AE} {ObjectID("5d7f6b2b57d5104f58e53d2c") [fa-AF ps uz-AF tk] Afghanistan 1149361 Kabul Afghani ? AFN AF}]
為什么追加會覆蓋子元素?
12345678_0001
2023-07-26 17:09:33