what is zero value of reflect.Value in golang - go

In golang reflect package, reflect.value.IsValid has those comments:
IsValid reports whether v represents a value. It returns false if v is the zero Value. If IsValid returns false, all other methods except String panic. Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.
Read above, i am very confused. i don't know what is zero value of reflect.Value and use it call isValid get false.
I write some code to test:
var haha *int
fmt.Println(reflect.ValueOf(haha).IsValid()) //true
var hehe interface{}
fmt.Println(reflect.ValueOf(hehe).IsValid() //false

A zero value is the default initialization value that is written to the memory address when no explicit initialization is provided. Tesing if v is a zero value is, in fact, testing if you have initialized v.
var haha *int returns true because haha is a pointer and cannot be created with an rational "default zero value", otherwise it would point to a random address in your memory and could have dangerous side effects. Pointers are nil when they are created, when other data types will have an actual value.
On the other hand, the zero value of var hehe interface{} is a nil *int, which is an actual entity.

Related

Get inteface's dynamic value when there is a pointer to integer as dinamic type

I'm struggling trying to figure out how to "extract" an integer value from a interface that holds a value whose dynamic type is pointer to integer.
You may follow this ready-to-run example in order to fully get the picture of what is problem and my intention.
TD;DR:
For some reason the reflection on a first call to reflect.ValueOf(value) over an interface whose dynamic type is pointer to integer, it returns a value whose Kind() results in reflect.Ptr, when i extract again the "inner" value using reflect.ValueOf(value).Elem() i get another value whose Kind() results in reflect.Struct. I don't even code any struct, but they appears by their own "magically". So golang may not panic but i don't understand what is happening, probably i lack of some information. I would be very thankful with anyone that explaint me what is going on.
This may work for this case, but it doesn't enlight me. I would expect to have an integer value "behind" the pointer. I don't need to access for modification, i only need to get it.
It's struct because when you call ToCompliantValue inside the function, you're not passing it the value itself, you're passing a reflect.Value value, and reflect.Value is a struct. You have to handle it directly instead of trying to use recursion:
v := reflect.ValueOf(value)
fmt.Printf("Value is %+v\n", v)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
// ...
Working example: https://play.golang.org/p/ndRjXJfj5xV

Transitive property in Golang

This piece of code looks quite counter-intuitive to me:
var first *byte
var second interface{}
fmt.Println(first, first == nil) // <nil> true
fmt.Println(second, second == nil) // <nil> true
fmt.Println(first == second) // false
As far as I understand, the case is that the first variable is a pointer to an empty variable of type byte, whereas the second is an empty interface. So, as the variables are not the same type, they are not considered equal.
But if they are not equal to each other, how can they be equal to some third value? Is it common situation in programming languages when Transitive Law is not held?
The identifier nil represents the zero value for channel, pointer, interface, function, map and slice types.
The zero value for a *byte is not equal to the zero value for an interface{}.
Nil does represent a zero value, but each value in go also has a type.
I have also seen something similar to this in testing when methods return a custom error type as err, and then check to see if the errors are equal which fails because Go intrinsically believes they are different because you have not type asserted it to the custom error type.
Here's a more interesting example of intransitivity of Go's == operator. It doesn't rely on nil; for types like functions and maps, x==nil is best thought of as an special operator, since values of those types aren't comparable to each other.
// "hello" != x == s == "hello"
func main() {
type S string
var s S = "hello"
var x interface{} = s
fmt.Println(s == "hello") // s == "hello"
fmt.Println(x == s) // x == s
fmt.Println(x == "hello") // x != "hello"
}
One way to explain this anomaly is that there are really two different equality operators at work here. The first comparison has type S, but the second and third have type interface{}. Once the types are erased, the interface conversions become explicit and each "==" is replaced by the appropriate comparison operator for its type. Those comparison operators, individually, are true equivalence relations, which is to say they are transitive.

When does reflect.IsValid return false?

I am curious about IsValid function, because during my use of this function, it never returned false. So when does it return a negative result?
As the doc reflect.IsValid() says:
It returns false if v is the zero Value. [...]
Most functions and methods never return an invalid value. If one does, its documentation states the conditions explicitly.
Value.IsValid() is supposed to report whether the reflect.Value itself is valid, not the value it wraps (if any).
All the examples below print false. You can try them on the Go Playground.
The simplest example is calling IsValid() on the zero value of reflect.Value (which is a struct):
fmt.Println(reflect.Value{}.IsValid())
The 2nd simplest example is when passing nil to reflect.ValueOf():
fmt.Println(reflect.ValueOf(nil).IsValid())
Another example: start with a pointer being nil, in this case there is no "pointed" value, a nil pointer points to nowhere. Attempting to get the reflect.Value of the pointed value using Value.Elem() results in a zero reflect.Value whose IsValid() method will return false:
var i *int
v := reflect.ValueOf(i)
v2 := v.Elem()
fmt.Println(v2.IsValid())
Or in one line:
fmt.Println(reflect.ValueOf((*int)(nil)).Elem().IsValid())
Same thing if you call Value.Indirect() on the above reflect.Value():
fmt.Println(reflect.Indirect(v).IsValid())
Or attempting to get a non-existing struct field by name using Value.FieldByName():
s := struct{}{}
fmt.Println(reflect.ValueOf(s).FieldByName("").IsValid())
Or attempting to get a non-existing method by name using Value.MethodByName():
fmt.Println(reflect.ValueOf(s).MethodByName("").IsValid())
Or attempting to get a value from a map by a non-existing key using Value.MapIndex():
m := map[int]int{}
fmt.Println(reflect.ValueOf(m).MapIndex(reflect.ValueOf(3)).IsValid())
The list goes on...

Golang reflection on interface vs pointer-to-interface

In the example of gob usage http://golang.org/src/encoding/gob/example_interface_test.go they provide the following thesis:
Pass pointer to interface so Encode sees (and hence sends) a value of interface type. If we passed p directly it would see the concrete type instead. See the blog post, "The Laws of Reflection" for background.
I've read The Laws of reflection twice, and a related Russ Cox article too. But I can't find a distinction between pointer-to-interface and interface there.
So why is it that through the pointer it sees a value of interface type, and with no pointer it sees (surprisingly to me) the concrete type?
It seems to me that the relevant part is this:
Continuing, we can do this:
var empty interface{}
empty = w
and our empty interface value empty will again contain that same pair, (tty, *os.File). That's handy: an empty interface can hold any value and contains all the information we could ever need about that value.
(emphasis added)
When you assign an interface value to a value of type interface{}, the "pointer to data" part of the new value doesn't point to the old value, but rather to the data old value was pointing to. We can prove that with a bit of unsafe code:
type iface struct {
Type, Data unsafe.Pointer
}
var r io.Reader = &bytes.Buffer{}
var i interface{} = r
rr := *(*iface)(unsafe.Pointer(&r))
ii := *(*iface)(unsafe.Pointer(&i))
fmt.Printf("%v\n", ii.Data == rr.Data) // Prints true.
On the other hand, if we use a pointer, it will point to the interface value itself. So now reflect can actually see, what interface exactly are we talking about. E.g.:
var i2 interface{} = &r
ii2 := *(*iface)(unsafe.Pointer(&i2))
fmt.Printf("%v\n", ii2.Data == unsafe.Pointer(&r)) // Prints true.
Playground: http://play.golang.org/p/0ZEMdIFhIj

golang - Elem Vs Indirect in the reflect package

From the docs
func (v Value) Elem() Value
Elem returns the value that the interface v contains or that the pointer v points to. It panics if v's Kind is not Interface or Ptr. It returns the zero Value if v is nil.
func Indirect(v Value) Value
Indirect returns the value that v points to. If v is a nil pointer, Indirect returns a zero Value. If v is not a pointer, Indirect returns v.
So can I safely assume the following?
reflect.Indirect(reflect.ValueOf(someX)) === reflect.ValueOf(someX).Elem().
Is Indirect method just a convenience method for the right hand side of the above?
If a reflect.Value is a pointer, then v.Elem() is equivalent to reflect.Indirect(v). If it is not a pointer, then they are not equivalent:
If the value is an interface then reflect.Indirect(v) will return the same value, while v.Elem() will return the contained dynamic value.
If the value is something else, then v.Elem() will panic.
The reflect.Indirect helper is intended for cases where you want to accept either a particular type, or a pointer to that type. One example is the database/sql conversion routines: by using reflect.Indirect, it can use the same code paths to handle the various types and pointers to those types.

Resources