第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

與 MongoDB go 客戶端一起使用時 bson struct 標(biāo)記的必要性

與 MongoDB go 客戶端一起使用時 bson struct 標(biāo)記的必要性

Go
有只小跳蛙 2022-04-26 19:58:20
我正在觀看有關(guān)如何創(chuàng)建使用 MongoDB 進(jìn)行持久性的 Go RESTful API 的教程(更準(zhǔn)確地說是這個)。講師在他的模型(結(jié)構(gòu))中同時使用 json和bson標(biāo)簽,例如type NoteUpdate struct {    ID        string `json:"id,omitempty" bson:"_id,omitempty"`    Title     string `json:"title" bson:"title,omitempty"`    Content   string `json:"content" bson:"content,omitempty"`    ChangedAt int64  `json:"changed_at" bson:"changed_at"`}但是官方的 go 驅(qū)動程序示例并沒有這樣做。事實(shí)上,根本沒有使用結(jié)構(gòu)標(biāo)簽。bson使用標(biāo)簽的目的/用處是什么?我想到的一件事是有人想要創(chuàng)建自定義 mongo_id字段,在這種情況下bson,應(yīng)該聲明與該結(jié)構(gòu)的字段的顯式映射。bson標(biāo)簽還有其他附加價值嗎?
查看完整描述

2 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個贊

MongoDB 驅(qū)動程序僅使用bson標(biāo)簽。json標(biāo)簽僅用于encoding/json包(或其他處理 JSON 編組/解組的第 3 方包)。

您不需要指定和使用bson標(biāo)簽,在這種情況下,驅(qū)動程序在編碼結(jié)構(gòu)值時通常只使用小寫的字段名稱。bson但是,當(dāng)您需要不同的名稱時,標(biāo)簽是必需的。

bson即使您想使用小寫的字段名稱,指定標(biāo)簽也是一個好習(xí)慣,因?yàn)橛袝r您可能需要重命名結(jié)構(gòu)字段,這會導(dǎo)致麻煩和不一致。如果您指定bson標(biāo)簽,那么將來是否重命名字段都沒有關(guān)系,它們?nèi)詫⒈痪幗M到相同的屬性中,并且解組它們將繼續(xù)起作用。


查看完整回答
反對 回復(fù) 2022-04-26
?
動漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個贊

bson 標(biāo)簽也可以包含改變默認(rèn)封送行為的標(biāo)志:


OmitEmpty  Only include the field if it's not set to the zero value for the type or to

           empty slices or maps.


MinSize    Marshal an integer of a type larger than 32 bits value as an int32, if that's

           feasible while preserving the numeric value.


Truncate   When unmarshaling a BSON double, it is permitted to lose precision to fit within

           a float32.


Inline     Inline the field, which must be a struct or a map, causing all of its fields

           or keys to be processed as if they were part of the outer struct. For maps,

           keys must not conflict with the bson keys of other struct fields.


Skip       This struct field should be skipped. This is usually denoted by parsing a "-"

           for the name.


查看完整回答
反對 回復(fù) 2022-04-26
  • 2 回答
  • 0 關(guān)注
  • 238 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號