Open
Description
Comparing two Value
type does not do what the user expects. The type is documented as such:
Using == on two Values does not compare the underlying values they represent, but rather the contents of the Value structs. To compare two Values, compare the results of the Interface method.
However, it is still common to see users accidentally make this mistake. What's worse is that it "seems to work", giving a false sense of security.
s1, s2 := "hello", "goodbye"
v1a := reflect.ValueOf(&s1)
v1b := reflect.ValueOf(&s1)
v2 := reflect.ValueOf(&s2)
fmt.Println(v1a == v1a) // Prints true
fmt.Println(v1a == v1b) // Prints true
fmt.Println(v1a == v2) // Prints false
This is because the Value
struct contains a flag
field whose value may be different for each Value
and is entirely an implementation detail of the reflect package.
Perhaps we should add a uncomparable type to prevent this misuse:
type Value struct {
_ [0]func() // Prevents Value from being comparable and occupies 0 bytes
typ *rtype
ptr unsafe.Pointer
flag
}
\cc @ianlancetaylor