Additionally, "ValueOf(nil) returns the zero Value." (docs for
reflect.ValueOf).
All the other variables you pass into ValueOf have a concrete type, so
the interface{}'s (type, concrete) pair contains a non-nil dynamic type,
making the interface{} value that ValueOf recieves non-nil.
Try putting each of those into an interface{} variable and test them ==
nil; you'll see that only the one that has nil dynamic type (the
interface{} variable) is actually equal to nil.
On 02/02/2015 11:13 AM, Péter Szilágyi wrote:
> There's a slight semantic you skipped over:
>
> IsValid returns true if v represents a value. It returns false if v
> is the zero *V*alue.
>
>
> Capital V. It checks whether v is or is not the zero value of
> reflect.Value, not some random value in general.
>
> On Mon, Feb 2, 2015 at 1:09 PM, gofake <
fab...@gmx.de
> <mailto:
fab...@gmx.de>> wrote:
>
> Hi there,
>
> I'm very new at go, so I hope my question will be very simple for you.
>
> I don't understand the meaning of the Zero Value in connection with
> IsValid.
>
> Excerpt from the Spec:
>
> When storage is allocated for a variable
> <
https://golang.org/ref/spec#Variables>, either through a
> declaration or a call of |new|, or when a new value is created,
> either through a composite literal or a call of |make|, and no
> explicit initialization is provided, the variable or value is
> given a default value. Each element of such a variable or value
> is set to the /zero value/ for its type
>
>
> Excerpt from reflect:
>
> IsValid returns true if v represents a value. It returns false
> if v is the zero Value.
>
>
> If I get it right, the zero Value is set by default when I allocate
> a new variable and IsValid checks if the value is the zero Value.
>
>
https://play.golang.org/p/pF09NBNXeR
>
> But why is only "myInterface" from the example false and all others
> true? If only an empty interface returns false, why do I need a
> special function to check this (I mean IsValid). Isn't it the same
> to check if myInterface == nil?
>
> I hope you can help me with my issue.
>
> Thanks!!!
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to
golang-nuts...@googlegroups.com
> <mailto:
golang-nuts...@googlegroups.com>.
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
golang-nuts...@googlegroups.com
> <mailto:
golang-nuts...@googlegroups.com>.