我想知道是否有任何方法可以使 stuct 字段即使不為空也不會(huì)提交給 mgo。我發(fā)現(xiàn)這樣做的唯一方法是使字段小寫,這使得訪問變得很痛苦。還有其他方法嗎?這是一個(gè)示例,我在這里的目標(biāo)是不將 SSN 提交到數(shù)據(jù)庫(kù)中,但仍然使用大寫字母。package mainimport ( "fmt" "crypto/sha1" "encoding/base64" "labix.org/v2/mgo")type Person struct{ Name string SSN string HashedSSN string}func main() { bob := Person{"Bob", "fake_ssn", ""} hasher := sha1.New() hasher.Write( []byte(bob.SSN)) sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) bob.HashedSSN = sha mgoSession, err := mgo.Dial("localhost:27017") if err != nil { fmt.Println("mongo_config#initMongoSessions : Could not dial to mgoSession", err) } else { mgoSession.DB("test").C("person").Insert(bob) }}謝謝,
即使不為空,Mgo 也會(huì)省略字段
喵喵時(shí)光機(jī)
2021-07-28 17:46:43