我想知道是否有一种方法可以使用结构体字段,即使它不为空也不会被提交到mgo。
我找到的唯一方法是将字段改为小写,这样访问时很麻烦。还有其他方法吗?
这只是一个例子,我的目标是不将社保号码提交到数据库中,但仍然希望保持它的大写形式。
package main
import (
"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)
}
}
谢谢,