今天在用 mgo 的时候,发现有一个 bug ,在读取数据并存放在结构之后,之前结构里设置的变量都被重置了。
go
package main import ( "fmt" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "os" ) type TestStruct struct { Id bson.ObjectId `bson:"_id"` testField int } func handleError(err error) { if err != nil { panic(err) os.Exit(1) } } func main() { session, err := mgo.Dial("localhost") handleError(err) c := session.DB("test").C("test") id := bson.NewObjectId() err = c.Insert(bson.M{ "_id": id, }) handleError(err) s := TestStruct{} s.testField = 10 println("Address of s", &s) println("Address of t", &s.testField) //这时 testField 的值为 10 fmt.Println(s.testField) // 在数据库里读取数据,复制给结构 s err = c.FindId(id).One(&s) handleError(err) println("Address of s", &s) println("Address of t", &s.testField) //这时 testField 的值为 0,testField 为私有变量 fmt.Println(s.testField) }
查看了一下 bson
的源码,发现他是通过 reflect
来赋值的,我写了一个小 Demo 发现确实可以使用 reflect 将私有成员设置为 0 。
go
package main import ( "fmt" "reflect" ) type TestStruct struct { testField int } func main() { data := TestStruct{} data.testField = 111 value := reflect.ValueOf(&data).Elem() dataType := value.Type() newData := reflect.New(dataType).Elem() fmt.Println("Type of Data", dataType) value.Set(newData) if data.testField == 0 { fmt.Println("testField setted to 0") } }
这作何解释?怎么避免私有变量被赋值?
func (Value) Set
func (v Value) Set(x Value)
Set assigns x to the value v. It panics if CanSet returns false. As in Go, x's value must be assignable to v's type.
像一楼说的那样。
你这段代码,实际上执行一个值的覆盖操作。相当于以下这段代码:
data := TestStruct{}
newData := TestStruct{}
data = newData
防君子不防小人,带反射的语言都可以这么干,有什么大惊小怪的?
java 也可以通过反射api把私有变量设置为公开访问的。。。。。