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

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

在 mgo 中定義 MongoDB 模式/集合

在 mgo 中定義 MongoDB 模式/集合

Go
繁華開滿天機(jī) 2021-12-13 18:52:45
我想使用 mgo 創(chuàng)建/保存 MongoDB 集合。但我想更廣泛地定義它(例如,提到一個屬性是強(qiáng)制性的,另一個是枚舉類型并具有默認(rèn)值)。我已經(jīng)定義了這樣的結(jié)構(gòu),但不知道如何描述它的約束。type Company struct {    Name        string `json:"name" bson:"name"` // --> I WANT THIS TO BE MANDATORY    CompanyType string `json:"companyType" bson:"companyType"` // -->I WANT THIS TO BE AN ENUM}這是否可以在 mgo 中完成,就像我們?nèi)绾卧?MongooseJS 中完成一樣?
查看完整描述

1 回答

?
炎炎設(shè)計(jì)

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

mgo 不是 ORM 或驗(yàn)證工具。mgo 只是 MongoDB 的一個接口。


自己做驗(yàn)證也不錯。


type CompanyType int


const (

  CompanyA CompanyType = iota // this is the default

  CompanyB CompanyType

  CompanyC CompanyType

)


type Company struct {

  Name string

  CompanyType string

}


func (c Company) Valid() bool {

  if c.Name == "" {

    return false

  }

  // If it's a user input, you'd want to validate CompanyType's underlying

  // integer isn't out of the enum's range.

  if c.CompanyType < CompanyA || c.CompanyType > CompanyB {

    return false

  }

  return true

}

檢查這個出了更多關(guān)于圍棋枚舉。


查看完整回答
反對 回復(fù) 2021-12-13
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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