亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將primitive.M轉換為映射[字符串]字符串然后轉換為字符串?

如何將primitive.M轉換為映射[字符串]字符串然后轉換為字符串?

Go
慕婉清6462132 2022-06-27 15:08:31
我們如何將原始 M 轉換為字符串?package mainimport (    "go.mongodb.org/mongo-driver/bson")func main() {    a := bson.M{ // primitive.M        "test": bson.M{            "fielda": "AAA",            "fieldb": "BBB",        },    }}我正在使用它來記錄進程中失敗的 mongodb 文檔。我能夠使用 打印值logrus.Error,我想將此轉換復制為字符串,然后將其記錄到文件中。// cursor = "go.mongodb.org/mongo-driver/mongo" *mongo.Cursor// logrus = "github.com/sirupsen/logrus"//...        var temp bson.M        _ := cursor.Decode(&temp)     // assume this is not returning error, it will log the map        logrus.Error("value: ", temp) // value: map[__v:0 _id:ObjectID(\"5c8ef7df7216e9935ecd7859\") field1:test]
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

最簡單的解決方案可能是這樣使用fmt.Sprint():


a := bson.M{

    "_id": primitive.NewObjectID(),

    "test": bson.M{

        "fielda": "AAA",

        "fieldb": "BBB",

    },

}


s := fmt.Sprint(a)

fmt.Println(s)

這將輸出(在Go Playground上嘗試):


map[_id:ObjectID("4af9f07018f18fbf63f00366") test:map[fielda:AAA fieldb:BBB]]


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號